Swift的界面传值------属性传值、协议传值、闭包、通知

属性传值


控制器一

import UIKit

classViewController:UIViewController{

    overridefuncviewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        self.title="控制器1"

        self.view.backgroundColor = UIColor.white


        letbut =UIButton();


        but.frame=CGRect.init(x:20,y:100,width:80,height:40);


        but.backgroundColor = UIColor.yellow;


        but.setTitle("切换", for:UIControlState.normal);

        but.addTarget(self, action:#selector(buttonOneClicked), for:UIControlEvents.touchUpInside);


       self.view.addSubview(but)


    }

    @objcfuncbuttonOneClicked(){



        letv2 =ViewController2();

        v2.str="2";


        self.navigationController?.pushViewController(v2, animated:true);


    }

    overridefuncdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

控制器二

import UIKit

classViewController2:UIViewController{

    var str =String()//创建一个属性,用来前后两个界面的传值


    overridefuncviewDidLoad() {

        super.viewDidLoad()

      self.view.backgroundColor = UIColor.red

        self.title=self.str

        // Do any additional setup after loading the view.

    }

    overridefuncdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

协议传值

视图控制器一


import UIKit

classViewController:UIViewController,transValue{

    overridefuncviewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        self.title="控制器1"

        self.view.backgroundColor = UIColor.white


        letbut =UIButton();


        but.frame=CGRect.init(x:20,y:100,width:80,height:40);


        but.backgroundColor = UIColor.yellow;


        but.setTitle("切换", for:UIControlState.normal);

        but.addTarget(self, action:#selector(buttonOneClicked), for:UIControlEvents.touchUpInside);


       self.view.addSubview(but)


    }

    @objcfuncbuttonOneClicked(){



        letv2 =ViewController2();

        v2.str="2";

        v2.delegate=self;


        self.navigationController?.pushViewController(v2, animated:true);


    }

    functransToFront(title:String) {

        self.title= title

    }

    overridefuncdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

视图控制器二

import UIKit

//创建协议

protocoltransValue {

    //反向传值

    functransToFront(title:String)


}

classViewController2:UIViewController{



    varstr =String()

    //创建遵守协议的对象

    vardelegate:transValue?




    overridefuncviewDidLoad() {

        super.viewDidLoad()

      self.view.backgroundColor = UIColor.red

        self.title=self.str

        // Do any additional setup after loading the view.



        letbut =UIButton();


        but.frame=CGRect.init(x:20,y:100,width:80,height:40);


        but.backgroundColor = UIColor.yellow;


        but.setTitle("切换", for:UIControlState.normal);

        but.addTarget(self, action:#selector(buttonOneClicked), for:UIControlEvents.touchUpInside);


        self.view.addSubview(but)


    }

    @objcfuncbuttonOneClicked(){




        delegate?.transToFront(title:"改变")

        self.navigationController?.popViewController(animated:true);


    }

    overridefuncdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

闭包(反向传值)

控制器一

import UIKit

classViewController:UIViewController{

    overridefuncviewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        self.title="控制器1"

        self.view.backgroundColor = UIColor.white


        letbut =UIButton();


        but.frame=CGRect.init(x:20,y:100,width:80,height:40);


        but.backgroundColor = UIColor.yellow;


        but.setTitle("切换", for:UIControlState.normal);

        but.addTarget(self, action:#selector(buttonOneClicked), for:UIControlEvents.touchUpInside);


       self.view.addSubview(but)


    }

    @objcfuncbuttonOneClicked(){



        letv2 =ViewController2();

        v2.changeTitle= { (title:String)in

            self.title= title


        }


        self.navigationController?.pushViewController(v2, animated:true);


    }


    overridefuncdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

控制器二

import UIKit

//创建协议

protocoltransValue {

    //反向传值

    functransToFront(title:String)


}

classViewController2:UIViewController{



    varstr =String()


    //定义闭包并带有一个参数

    varchangeTitle :((_title:String)->Void)?




    overridefuncviewDidLoad() {

        super.viewDidLoad()

      self.view.backgroundColor = UIColor.red

        self.title=self.str

        // Do any additional setup after loading the view.



        letbut =UIButton();


        but.frame=CGRect.init(x:20,y:100,width:80,height:40);


        but.backgroundColor = UIColor.yellow;


        but.setTitle("切换", for:UIControlState.normal);

        but.addTarget(self, action:#selector(buttonOneClicked), for:UIControlEvents.touchUpInside);


        self.view.addSubview(but)


    }

    @objcfuncbuttonOneClicked(){




        changeTitle?("改变")

        self.navigationController?.popViewController(animated:true);


    }

    overridefuncdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

通知

发送通知:NSNotificationCenter .defaultCenter().postNotificationName("NotificationCenter ", object: dict, userInfo: dict)

接受通知:NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(getMyName(_:)), name: "NotificationCenter", object: nil)

删除通知:NSNotificationCenter.defaultCenter().removeObserver(self)或者是NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationCenter", object: nil)

你可能感兴趣的:(Swift的界面传值------属性传值、协议传值、闭包、通知)