美文网首页程序员
Swift 多种页面传值

Swift 多种页面传值

作者: 君莫叹人生如若初见 | 来源:发表于2016-10-19 15:08 被阅读975次

    Swift 日渐成熟,如今已经发展到3.0 版本。本文介绍iOS开发中常用的技能---页面传值。(PS:熟知OC更容易理解)

    直接通过控制器提供变量传值

    首先我们创建两个控制器,命名为ViewController, FirstViewController。
    下面是ViewController(根控制器)的代码:

    //懒加载一个btn
     lazy private var nextPage:UIButton! = {
            let nextPage = UIButton.init(type: UIButtonType.roundedRect)
            nextPage.backgroundColor = UIColor.red
            nextPage.center = CGPoint.init(x: 0, y: 300)
            return nextPage
        }()
    
    //添加btn
     nextPage.setTitle("跳转到下一页面", for: .normal)
            nextPage.addTarget(self, action: #selector(pushToPage(_:)), for: .touchUpInside)
            nextPage.sizeToFit()
            view.addSubview(nextPage)
    //添加事件
     func pushToPage(_ button:UIButton) {
            let otherPage = FirstViewController()
            otherPage.labelText = "从上一个页面传来的值"
            navigationController?.pushViewController(otherPage, animated: true)
        }
    

    FirstViewController 里面则加载一个label,用于显示传递过来的值。

    //在FirstVC懒加载一个label
       lazy  private var label:UILabel! = {
            let label = UILabel()
            label.backgroundColor = UIColor.blue
            label.center = CGPoint.init(x: 200, y: 300)
            return label
        }()
    //在FirstVC里面设置一个变量
     var labelText:String?
    

    此时,则可以通过在根控制器中push事件来得到传递来的值。

    otherPage.labelText = "从上一个页面传来的值"
    

    上一个方法是最简单的方法。接下来以上述方法为基础,讲述一下协议传值。

    协议传值(回传)

    在上述的ViewController(根控制器)中创建一个协议(代理)。

    //创建一个协议
    protocol returnValueDelegate{
        func returnValue(params:String)
    }
    
    

    并且遵从代理

    遵从代理

    接下来在根视图中添加一个label控件来显示传递过来的值。

      lazy  private var firstlabel:UILabel! = {
            let label = UILabel()
            label.backgroundColor = UIColor.blue
            label.center = CGPoint.init(x: 200, y: 300)
            return label
        }()
    

    在传递另一页面的事件中添加协议

     otherPage.returnValueDelegate = self
    

    此时,即可以通过回传协议的值来得到当前的值了,FirstVC中

     func toLastPage(_ button: UIButton){
            returnValueDelegate?.returnValue(params: "我是姚")
            _ = navigationController?.popViewController(animated: true)
        }
    

    通知传值

    通知传值也是比较简单的,如下所示
    在根控制器中先添加通知的方法

      //添加通知
            NotificationCenter.default.addObserver(self, selector: #selector(notificationValue(_:)), name: NSNotification.Name(rawValue: "sendValue"), object: nil)
    //事件,此时secLabel是用来显示通知传递来的值,此不在赘述
     func notificationValue(_ notification:Notification) {
            secLabel.text = notification.object as! String?
        }
    

    接下来在第二个界面中发送通知即可。

     NotificationCenter.default.post(name: NSNotification.Name.init("sendValue"), object: "我是传递过来的值")
    

    注意:通知使用的一个好习惯,随时移除他,在第一个界面移除通知:

      deinit {
            print("deinit \(String.init(describing: self.classForCoder))")
            NotificationCenter.default.removeObserver(self)
        }
    

    闭包传值

    闭包其实和oc中的block是一个差不多的语法。理解block对于理解Closure有更好的思维方式。
    我们在FirstVC中设置一个type:

    //Closure传值
    typealias ValueClosure = (String)->Void
    

    随后在私有变量中提供Closure变量

    var backValueClosure : ValueClosure?
    
    //在回调事件中传值
       if self.backValueClosure != nil {
                self.backValueClosure!("闭包传递来的值")
            }
    

    此时在根视图中进行解析:

    //从这个视图中即可得到值
      otherPage.backValueClosure = {(bacString:String)-> Void  in
                self.thidLabel.text = bacString
            }
    

    总结:以上几种方式是iOS常见的页面传值形式,当然还可能有其他传值(UserDefaults 单例等),但是在日常开发有这些传值应该就可以解决大部分的问题。我们需要注意的是,通知,回调,代理传值都有其中的利弊,以前有很多文章已经谈到,在此不再赘述,主要是界面的回传,顺传的话,直接设置变量即可。

    相关文章

      网友评论

        本文标题:Swift 多种页面传值

        本文链接:https://www.haomeiwen.com/subject/sfjxuttx.html