Swift界面传值

作者: 师弟出马 | 来源:发表于2016-04-01 16:50 被阅读821次

    Swift中界面传值的方法 主要有三种

    1.代理传值
    2.闭包传值(即OC中的Block)

    1. 属性传值

    代理传值

    First页面

    class FirstViewController: UIViewController ,ValueDelegate {
    
        //设置属性label   后面加个!  代表在需要的时候再初始化
        var label : UILabel!
        override func viewDidLoad() {
            super.viewDidLoad()
     //设置导航视图控制器的右边按钮
      //在Swift中设置枚举值时候使用的是枚举类名 + . 枚举名    在这里系统帮我们自动省略掉类名  
    //在设置事件的时候 ("方法名")
     self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
      
            //初始化label的位置
            label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
            //设置label的背景颜色
            label.backgroundColor = UIColor.cyanColor()
            //将label添加到视图上
            self.view.addSubview(label)
        }
        
        //声明导航视图控制器的按钮  点击事件
        func jumpToSecondVCClick() {
            let secondVC = SecondViewController()
            
            //设置代理
            secondVC.delegate = self
            //跳转到第二个页面
            self.showViewController(secondVC, sender: nil)
        }
        
    
        //实现代理方法
        func valueClicked(string: String) {
            label.text = string
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
         
        }
        
    }
    

    Second页面

    
    //声明协议   同OC相同 还是需要写到类的上面
    
    protocol ValueDelegate {
        //声明代理方法
        func valueClicked(string : String)
    }
    
    class SecondViewController: UIViewController  {
    
        //设置代理属性  必须置为nil
        var delegate : ValueDelegate? = nil
       
        //设置输入框属性
        var TF : UITextField!
        override func viewDidLoad() {
            super.viewDidLoad()
            //设置页面的颜色
            self.view.backgroundColor = UIColor.whiteColor()
    
    //设置导航视图控制器的左边按钮        
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")
    
           //初始化输入框并设置frame
           TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
            TF.backgroundColor = UIColor.cyanColor()
            //将输入框添加到视图上
            self.view.addSubview(TF)
        }
        
       //设置跳转时间
        func jumpToFristVCClick() {
    
    //当跳转到Frist页面的时候  设置代理将输入款输入的文字传到First页面        
    self.delegate?.valueClicked(TF.text!)
    
    //跳转到First页面        
    self.navigationController?.popViewControllerAnimated(true)
            
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
    

    闭包传值

    关于UI的代码与上面的一模一样 只是传值的方式不一样,在下面就不添加过多的注释了

    First页面

    class FirstViewController: UIViewController{
    
        var label : UILabel!
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
            label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
            label.backgroundColor = UIColor.cyanColor()
            self.view.addSubview(label)
        }
        
        func jumpToSecondVCClick() {
            let secondVC = SecondViewController()
            //将传过来的值  赋给label
            secondVC.sendValueClsure = { (string : String) -> Void in
                self.label.text = string
            }
            self.showViewController(secondVC, sender: nil)
        }
        
        
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    
    

    Second页面

    //重命名一个闭包
    typealias sendValue = (string : String) -> Void
    
    class SecondViewController: UIViewController  {
    
        //创建一个闭包属性
        var sendValueClsure : sendValue?
        var TF : UITextField!
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.whiteColor()
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")
           TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
            TF.backgroundColor = UIColor.cyanColor()
            self.view.addSubview(TF)
        }
        
        func jumpToFristVCClick() {
            //将值附在闭包上,传到First页面
            self.sendValueClsure!(string: TF.text!)
            self.navigationController?.popViewControllerAnimated(true)
            
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    属性传值
    UI与上两个基本一样,将First页面的UILabel换成UITextField 即可

    Frist页面

    
    
    class FirstViewController: UIViewController{
    
        
        var TF : UITextField!
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
            TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
            TF.backgroundColor = UIColor.cyanColor()
    
            self.view.addSubview(TF)
        }
        
        
        
        func jumpToSecondVCClick() {
            let secondVC = SecondViewController()
            //将输入框中输入的文字赋值给Second控制器的属性string
            secondVC.string = TF.text
            self.showViewController(secondVC, sender: nil)
        }
        
        
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    }
    

    Second页面

    class SecondViewController: UIViewController  {
    
        var string : String? = nil
        var TF : UITextField!
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.whiteColor()
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")
           TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
            TF.backgroundColor = UIColor.cyanColor()
    
            //将从First页面传回来的string的值赋给Second的TF
            TF.text = string
            self.view.addSubview(TF)
        }
        
        func jumpToFristVCClick() {
            self.navigationController?.popViewControllerAnimated(true)
            
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
    

    在这三天的Swift学习中 ,我发现 在使用时 ,其实大部分用到了OC中的方法和思想,只是Swift相比OC的代码格式上更加简洁和易记
    由此可见,如果有OC基础的学习者们,在学习Swift的时候,只需要研究下Swift和OC的格式区别对于上手Swift还是相当快的

    相关文章

      网友评论

        本文标题:Swift界面传值

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