swift-传值

delegate,通知,闭包
//2个界面传值
界面1

/传值
//需求2个类传值传值有通知,属性,delegate,闭包;
import UIKit
//遵守代理
class ViewController: UIViewController,TwoMessageDelegate {


    let textlable = UILabel();
     let TwoVc = TwoViewController();



    override func viewDidLoad() {
        super.viewDidLoad()


        self.title = "首页";

        textlable.frame = CGRect(x:50,y:100,width:self.view.frame.size.width-100,height:30);
        textlable.textColor = UIColor.black;
        textlable.textAlignment = NSTextAlignment.center;
        textlable.backgroundColor = UIColor.red;
        textlable.text = "我要这这段话传给另一个界面"
       // textlable.frame = (x:100, y:100,witdh:self.view.frame.size.width,height:30);

        self.view .addSubview(textlable);


        let btn = UIButton();
        btn.frame = CGRect(x:50,y:150,width:100,height:30);
        btn.setTitle("跳转下一页", for: UIControlState.normal);
        btn.setTitleColor(UIColor.blue, for: UIControlState.normal);
        btn.addTarget(self, action:#selector(btnOnlick), for: UIControlEvents.touchUpInside);
        self.view .addSubview(btn);
      //闭包传值
        self.TwovcClouse();
      //通知传值
        self.TwoVCNotiftion();

    }


    //点击事件
    @objc func btnOnlick(){

        //属性传值
        TwoVc.string = self.textlable.text!;
        //
        TwoVc.delegate = self;
        self.navigationController?.pushViewController(TwoVc, animated: true);
        //静态跳转
//        self.present(<#T##viewControllerToPresent: UIViewController##UIViewController#>, animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)


    }


    //界面二代理实现
    func getMessage(controller: TwoViewController, string: String) {
        self.textlable.text = string;

    }
    //界面二闭包传值
     func TwovcClouse(){

        //闭包
        TwoVc.twoVcClouse = {(_ text:String) ->Void in
            print("闭包传过来的值为\(text)");
            self.textlable.text = text;
        }
    }

    func TwoVCNotiftion(){
        let NotifTwoVCaiton = NSNotification.Name(rawValue:"TWoVc");
        NotificationCenter.default.addObserver(self, selector: #selector(upDataChange(notif:)), name: NotifTwoVCaiton, object: nil);
    }

    //接收到后执行的方法
    @objc func upDataChange(notif: NSNotification) {
        guard let text: String = notif.object as! String? else { return }
        self.textlable.text = text;
    }
//移除通知
    deinit {
        NotificationCenter.default.removeObserver(self);
    }

界面2

//反向传值:1用delegate,和闭包
import UIKit
//代理步骤1
protocol TwoMessageDelegate:NSObjectProtocol
{
    //步骤2定义生命函数
    func getMessage(controller:TwoViewController,string:String)


}

//闭包
typealias TwoViewControllerClosure = (_ text:String) ->Void

class TwoViewController: UIViewController {

    var string:String = "";
    //代理变量
    var delegate:TwoMessageDelegate?;
    //闭包变量
    var twoVcClouse:TwoViewControllerClosure?;
    //通知
    let NotifMycation = NSNotification.Name(rawValue:"TWoVc");

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "界面二";
        let textlable = UILabel();

        textlable.frame = CGRect(x:50,y:100,width:self.view.frame.size.width-100,height:30);
        textlable.textColor = UIColor.black;
        textlable.textAlignment = NSTextAlignment.center;
        textlable.backgroundColor = UIColor.red;
        if string.isEmpty {
            print("空")
            textlable.text = "字符串为空";
        }else{
              textlable.text = self.string;
        }

        self.view .addSubview(textlable);


        let btn = UIButton();
        btn.frame = CGRect(x:50,y:150,width:100,height:30);
        btn.setTitle("返回上一页", for: UIControlState.normal);
        btn.setTitleColor(UIColor.blue, for: UIControlState.normal);
        btn.addTarget(self, action:#selector(btnOnlick), for: UIControlEvents.touchUpInside);
        self.view .addSubview(btn);




    }
   //传值给上一个界面用闭包和代理分别试试
    @objc func btnOnlick(){
        //代理写法
       // self.TwoVCdeleage();

        //闭包写法
        //self.TwoVcClosure();

        //通知
        self.TwoVcNotiftiaction();

        self.navigationController?.popViewController(animated: true);
        //静态返回
        //self.dismiss(animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)
    }

    func TwoVCdeleage() {

                if delegate != nil {
                    //传值
                    delegate?.getMessage(controller: self, string: "我是界面二传给你的值��");
                }

    }

    func TwoVcClosure() {
                if twoVcClouse != nil  {
                    twoVcClouse!("我是用闭包传值的");
                }

    }
    func TwoVcNotiftiaction(){

        //单个值
        NotificationCenter.default.post(name: NotifMycation, object:"我是通知给你传的值收到木")
        //多个值用(name: NotifMycation, object: nil, userInfo: ["" : ""])
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值