1. 程式人生 > >【swift_4】swift之代理傳值(delegate的用法)

【swift_4】swift之代理傳值(delegate的用法)

2017.03.16更新簡潔版

protocol ChildDelegate:class{
    func childDidSomething()}
classChild{
    weak vardelegate:ChildDelegate?}
delegate?.childDidSomething()

-----------莫名其妙的分割線---------

rootViewController

class ViewController: UIViewController,GetMessageDelegate
{
    var _button:UIButton?
    var _label:UILabel?

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.title = "RootViewController"
        //建立label 用來接收傳過來的值
        _label = UILabel(frame: CGRect(x: 50, y: 100, width: 220, height: 44))
        _label?.text = "get message from next page"
        
        _label?.textAlignment = NSTextAlignment.Center
        _label?.backgroundColor = UIColor.cyanColor()
        self.view.addSubview(_label!)
        
        //建立button 點選跳轉到下一個介面
        _button = UIButton(frame:CGRect(x:60,y:200,width:200,height:44))
        _button?.setTitle("go to next page", forState: UIControlState.Normal)
        _button?.setTitleColor(UIColor.yellowColor(), forState: UIControlState.Normal)
        _button?.backgroundColor = UIColor.blueColor()
        _button?.addTarget(self, action: "nextPage", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(_button!)
    }

    //push
    func nextPage()
    {
        let nextVC = NextViewController()
        //指定代理
        nextVC.delegate = self
        self.navigationController!.pushViewController(nextVC,animated:true)
    }
    //接收傳過來的值
    func getMessage(controller:NextViewController,string:String)
    {
        _label?.text = string
        if(string == "")
        {
            _label?.text = "null"
        }
 
    }


}

secondViewController

//建立協議
protocol GetMessageDelegate:NSObjectProtocol
{
    //回撥方法 傳一個String型別的值
    func getMessage(controller:NextViewController,string:String)
}
class NextViewController: UIViewController
{
    var delegate:GetMessageDelegate?
    var _textField:UITextField?
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.title = "SecondViewController"
        self.view.backgroundColor = UIColor.whiteColor()
        
        //建立textField 用來輸入要傳的值
        _textField = UITextField(frame: CGRect(x: 60, y: 100, width: 200, height: 44))
        _textField?.borderStyle = UITextBorderStyle.RoundedRect
        _textField?.placeholder = "input sth to send back"
        self.view.addSubview(_textField!)

        //建立返回的button
        var myButton = UIButton(frame:CGRect(x:60,y:200,width:200,height:44))
        myButton.center = CGPointMake(160,200)
        myButton.setTitle("send message back",forState:.Normal)
        myButton.addTarget(self,action:"goBack",forControlEvents:.TouchUpInside)
        myButton.backgroundColor = UIColor.blueColor()
        self.view.addSubview(myButton)
    }

    func goBack()
    {
        //呼叫代理方法
        if((delegate) != nil)
        {
            delegate?.getMessage(self,string:_textField!.text)
            self.navigationController?.popToRootViewControllerAnimated(true)
        }
        
    }
}