ãSwiftãdelegateãå®å ¨å æããããã®ã¾ã¨ã
iOSã¢ããªéçºã«é¢ããã¨å¿
ãã¨è¨ã£ã¦è¯ãã»ã©è³ã«ããdelegate(ããªã²ã¼ã)ããããåããã«ããã¦è¦ããã 人ã¯å¤ãã®ã§ã¯ãªãã§ãããããä»ã¾ã§UITableView
ã¨ãUIScrollView
ã使ã£ã¦ä½ã¨ãªãdelegate(ããªã²ã¼ã)ã®å®è£
ã¯ãã£ãäºã¯ãããã©èªä½ã§delegate(ããªã²ã¼ã)ãå®è£
ããæã«å°ã£ãå ´åãªããã«åèã«ãã¦ã¿ã¦ä¸ããã
ããããdelegate(ããªã²ã¼ã)ã£ã¦ãªã«
The Swift Programming Language (Swift 3.0.1): Protocols
Delegation
Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type. This design pattern is implemented by defining a protocol that encapsulates the delegated responsibilities, such that a conforming type (known as a delegate) is guaranteed to provide the functionality that has been delegated. Delegation can be used to respond to a particular action, or to retrieve data from an external source without needing to know the underlying type of that source.
ç°¡åã«è¨ãã¨ããã¯ã©ã¹ããå¦çã®ä¸é¨ãä»ã®ã¯ã©ã¹ã«ä»»ããããä»ã®ã¯ã©ã¹ã¸ã¡ãã»ã¼ã¸ãéãçã®ç®çã§ãã使ããããã¶ã¤ã³ãã¿ã¼ã³ã§ãã
ãã¶ã¤ã³ãã¿ã¼ã³ã¯ããèãè¨èã§ãããGoFãªããã§æåãªããã§ãã
ããããã¯ãµã³ãã«ãä½æãã¦å®éã®ä½ææ¹æ³ãè¨è¼ãã¾ãã
ç°å¢ xcode7.0
ã¾ãæµãã¨ãã¦
- SampleView(éç¥å )ã«ãããã³ã«ãç¨æããã
- SampleView(éç¥å )ã§ããªã²ã¼ãã¡ã½ãããå¼ã¶(å¦çãããªã²ã¼ãã¤ã³ã¹ã¿ã³ã¹ã«å§è²ãã)
- ViewController(éç¥å )ã§SampleView(éç¥å )ã§å®ç¾©ããããªã²ã¼ãã¡ã½ãããå®è£ ããã
ããªã²ã¼ããå®è£
ããæã«èãããã¨ã¯ä½ãå§è²ããã(ãªãã®å¦çãå¼ã°ããã®ã)ã決ããã ãã§ããã¶ã£ã¡ãã誰ã«å§è²ããã(ã©ãããå¼ã¶ã®ã)ã¯èããªãã¦è¯ãã§ãã
SampleView(éç¥å ã¯ã©ã¹)ã®ç¨æ
import UIKit class SampleView: UIView { override func drawRect(rect: CGRect) { let button = UIButton() button.setTitle("Tap", forState: .Normal) button.frame = CGRectMake(0, 0, 50, 50) button.backgroundColor = UIColor.redColor() button.addTarget(self, action: "tappedButton:", forControlEvents:.TouchUpInside) self.addSubview(button) self.backgroundColor = UIColor.blackColor() } @IBAction func tappedButton(sender: AnyObject) { self.backgroundColor = UIColor.greenColor() } }
ã¾ãã¯ãprotocol(ãããã³ã«)ã®å®è£
SampleView(éç¥å ã¯ã©ã¹)
import UIKit // SampleViewDelegate ãããã³ã«ãè¨è¿° @objc protocol SampleViewDelegate { // ããªã²ã¼ãã¡ã½ããå®ç¾© func didChangeBackgroundColor(str:String) } class SampleView: UIView { //SampleViewDelegateã®ã¤ã³ã¹ã¿ã³ã¹ãå®£è¨ weak var delegate: SampleViewDelegate? override func drawRect(rect: CGRect) { let button = UIButton() button.setTitle("Tap", forState: .Normal) button.frame = CGRectMake(0, 0, 50, 50) button.backgroundColor = UIColor.redColor() button.addTarget(self, action: "tappedButton:", forControlEvents:.TouchUpInside) self.addSubview(button) self.backgroundColor = UIColor.blackColor() } @IBAction func tappedButton(sender: AnyObject) { self.backgroundColor = UIColor.greenColor() } }
éç¥å ã¯ã©ã¹ã§ããªã²ã¼ãã¡ã½ãããå¼ã¶(å¦çãããªã²ã¼ãã¤ã³ã¹ã¿ã³ã¹ã«å§è²ãã)
import UIKit // SampleViewDelegate ãããã³ã«ãè¨è¿° @objc protocol SampleViewDelegate { // ããªã²ã¼ãã¡ã½ããå®ç¾© func didChangeBackgroundColor(str:String) } class SampleView: UIView { //SampleViewDelegateã®ã¤ã³ã¹ã¿ã³ã¹ãå®£è¨ weak var delegate: SampleViewDelegate? override func drawRect(rect: CGRect) { let button = UIButton() button.setTitle("Tap", forState: .Normal) button.frame = CGRectMake(0, 0, 50, 50) button.backgroundColor = UIColor.redColor() button.addTarget(self, action: "tappedButton:", forControlEvents:.TouchUpInside) self.addSubview(button) self.backgroundColor = UIColor.blackColor() } @IBAction func tappedButton(sender: AnyObject) { self.backgroundColor = UIColor.greenColor() // ããªã²ã¼ãã¡ã½ãããå¼ã¶(å¦çãããªã²ã¼ãã¤ã³ã¹ã¿ã³ã¹ã«å§è²ãã) self.delegate?.didChangeBackgroundColor("ã°ãªã¼ã³") } }
ããã¾ã§ã§éç¥å
ã¯ã©ã¹ã®å®è£
ã¯çµããã
éç¥å ã¯ã©ã¹ã§éç¥å ã§å®ç¾©ããããªã²ã¼ãã¡ã½ãããå®è£ ããã
ViewController(éç¥å ã¯ã©ã¹)
import UIKit // éç¥å´ã§å®ç¾©ãããããã³ã«(SampleViewDelegate)ã®ç¶æ¿ class ViewController: UIViewController,SampleViewDelegate { override func viewDidLoad() { super.viewDidLoad() let view = SampleView(frame: CGRectMake(100,100,100,100)) // SampleViewãã¤ã³ã¹ã¿ã³ã¹åãããã¿ã¤ãã³ã°ã§ããªã²ã¼ãã¤ã³ã¹ã¿ã³ã¹ã«selfãã»ãã view.delegate = self self.view.addSubview(view) } // SampleView DelegateMethod // SampleViewã®BackgroundColorãå¤æ´ãããäºãéç¥ func didChangeBackgroundColor(str:String) { print(str); } }
SampleView
ã®ãã¿ã³ãã¿ãããã度ã«ViewController
ã® didChangeBackgroundColor
ãå¼ã°ãã¦ããã®ãåããã¨æãã¾ãã
ããã¾ã§ã§delegateã®æµãã¯çµäºã§ãã
ãã¾ã
ä¸è¨ã®ããã«ä¿®é£¾åãªãã§ããªã²ã¼ãã¡ã½ãããå®ç¾©ããã¨ãéç¥å
ã§å¿
ãå®è£
ããªããã°ãããªãã¡ã½ããrequired
ã«ãªãã¾ããéç¥å´ã§required
ã§å®ç¾©ãããã¡ã½ãããéç¥å
ã§è¨è¿°ããªããã°ã¨ã©ã¼ã«ãªã£ã¦ãã¾ãã¾ãã
ããªã²ã¼ãã¡ã½ããå®ç¾©ã®éã«optional
修飾åãè¨è¿°ããã¨å¿
ãããå®è£
ããªãã¦ãè¯ãã¡ã½ããã«ããäºãã§ãã¾ãã
// SampleViewDelegate ãããã³ã«ãè¨è¿° @objc protocol SampleViewDelegate { // ããªã²ã¼ãã¡ã½ããå®ç¾© optional func didChangeBackgroundColor(str:String) } class SampleView: UIView { //SampleViewDelegateã®ã¤ã³ã¹ã¿ã³ã¹ãå®£è¨ weak var delegate: SampleViewDelegate? override func drawRect(rect: CGRect) { let button = UIButton() button.setTitle("Tap", forState: .Normal) button.frame = CGRectMake(0, 0, 50, 50) button.backgroundColor = UIColor.redColor() button.addTarget(self, action: "tappedButton:", forControlEvents:.TouchUpInside) self.addSubview(button) self.backgroundColor = UIColor.blackColor() } @IBAction func tappedButton(sender: AnyObject) { self.backgroundColor = UIColor.greenColor() // ããªã²ã¼ãã¡ã½ãããå¼ã¶(å¦çãããªã²ã¼ãã¤ã³ã¹ã¿ã³ã¹ã«å§è²ãã) self.delegate?.didChangeBackgroundColor?("ã°ãªã¼ã³") } }
import UIKit class ViewController: UIViewController,SampleViewDelegate { override func viewDidLoad() { super.viewDidLoad() let view = SampleView(frame: CGRectMake(100,100,100,100)) view.delegate = self self.view.addSubview(view) } }
optional
修飾åãè¨è¿°ããã°func didChangeBackgroundColor(str:String)
ãå¼ã°ãªãã¦ãã¨ã©ã¼ã«ãªããªãã
delegateã¯obj-cã®é ããé »ç¹ã«ä½¿ããã¦ãã¾ããããæè¿swiftã«ç§»è¡ãé²ãã§æ¹ãã¦delegateãå確èªããã¤ããã§ã¾ã¨ãã¦ã¿ã¾ããã