swift传值_present传值 swift - CSDN
精华内容
参与话题
  • Swift的四种传值方式总结

    千次阅读 2018-10-31 19:11:20
    Swift 的四种传值方式 有 单例, 代理,闭包(oc的block),通知. 1 单例 Swift 的单例简单到怀疑他的正确性.步骤如下:(下面有具体代码)  第一步 创建 SingleTest static let shareInstance = SingleTest()  第二步 ...

    Swift 的四种传值方式 有 单例, 代理,闭包(oc的block),通知.

    1 单例 Swift 的单例简单到怀疑他的正确性.步骤如下:(下面有具体代码)

     第一步 创建 SingleTest static let shareInstance = SingleTest()
     第二步 使用 SingleTest.shareInstance.getString()
     第三步 验证是否唯一 let str = SingleTest.shareInstance
     print(Unmanaged.passUnretained(str).toOpaque() ) // 打印内存地址

    2 代理基本和oc差不多.步骤如下:(下面有具体代码)

    第一步 定义协议  

    protocol dwDelegate {//第一步 定义协议
        func changeString(newString: String)
    }

     第二步  声明协议

    var delegate: dwDelegate?  // 第二步  声明协议

    第三步 触发协议

    self.delegate!.changeString(newString: "hahaha") // 第三步 触发协议

     第四步: 遵守协议

    class ViewController: UIViewController, dwDelegate { // 第四步: 遵守协议

     第五步,遵守协议

    vc.delegate = self // 第五步,遵守协议

     第六步: 实现协议

    func changeString(newString: String) { // 第六步: 实现协议
            print(newString)
        }

    3 闭包 具体步骤 如下    :(下面有具体代码)

     第一步  定义闭包

    typealias swiftBlock = (_ str: String) -> Void

     第二步   声明

    var callBack : swiftBlock? //第二步 声明

     第三步   实现闭包

    func callBackBlock(_ block: @escaping swiftBlock) { // 第三步 实现闭包
            callBack = block
        }

     第四步   触发闭包

    if callBack != nil {  // 第四步 触发闭包
                callBack!("闭包传值")
            }

    第五步   触发闭包回调

    cusVC.callBackBlock { (str) in  //第五步 触发闭包回调
                print(str)
            }

    4 通知 具体步骤如下

     第一步 第一步 创建通知 (用于接收通知)

    let center =  NotificationCenter.default

    第二步 添加观察者

    center.addObserver(self, selector: #selector(receiveValue(info:)), name: NSNotification.Name(rawValue: "passValue"), object: nil)

      第三步 通知触发的方法

    let str = info.object!
            print(str)
            let dic = info.userInfo! as Dictionary
            print(dic["age"]!)

     第四步 创建观察者(用于发送通知)

    let center = NotificationCenter.default // 第四步 创建观察者(用于发送通知)

    第五步 发出通知

    let dic = [
                "name" : "李四" ,
                "sex" : "男",
                "age" : "27"
            ]
            // 第五步 发出通知
            center.post(name: NSNotification.Name(rawValue: "passValue"), object: "传值", userInfo: dic)

     

    1.单例的具体代码:

    ViewController :

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.white
            let str = SingleTest.shareInstance
            print(Unmanaged.passUnretained(str).toOpaque() ) // 打印内存地址  
            // 单例的使用
            print(SingleTest.shareInstance.getString())
            
            //全局的一些东西.
            // 登陆成功记住用户名
            username = "bdgt"
            print(username)
            
            // 打印枚举
            print(alldata.loginUrl.rawValue)
            
            makeBtn()
            // Do any additional setup after loading the view, typically from a nib.
        }
        func makeBtn() {
            let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
            btn.setTitle("btn", for: UIControlState.normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
            self.view.addSubview(btn)
        }
        
        @objc func btnClick(){
        print("ddd")
            let vc = SecondViewController.init()
            present(vc, animated: true, completion: nil)
        }
        
        func changeString(newString: String) { // 第六步: 遵守协议
            print(newString)
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
    

    SecondViewController:

    import UIKit
    
    class SecondViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let str = SingleTest.shareInstance
            print(Unmanaged.passUnretained(str).toOpaque() )  // 打印内存地址
            
            
            self.view.backgroundColor = UIColor.lightGray
            makeBtn()
            // Do any additional setup after loading the view.
        }
        
        func makeBtn() {
            let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
            btn.setTitle("返回", for: UIControlState.normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
            self.view.addSubview(btn)
        }
        
        @objc func btnClick(){
            print("ddd")
            dismiss(animated: true) {
                print("1")
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }

    SingleTest:

    
    import UIKit
    
    var username = "" //全局的,可以存储一些必备的.
    
    enum alldata : String{  // 枚举  可以存储 接口
        case loginUrl = "http://www.dansdasda/login"
        case right = "右边"
    }
    
    class SingleTest {
        static let shareInstance = SingleTest()
        
        func getString() -> String {
            // 例如  获取当前的时间, 按照一定的格式,在这里写好之后, 所有的地方就都可以调用这里的了.
            return "共有方法,此处获取数据"
        }
    }
    
    /*第一种,类常量 Class constant , 建议使用    支持赖加载 线程安全
     class Singleton  {
     static let sharedInstance = Singleton()
     }
     */
    
    /*第二种  嵌套结构体变量格式
     class Singleton {
     class var sharedInstance: Singleton {
     struct Static {
     static let instance: Singleton = Singleton()
     }
     return Static.instance
     }
     }
     */
    /* 第三种最不建议,但是最想oc 的创建方式  dispatch_once
     class Singleton {
     class var sharedInstance: Singleton {
     struct Static {
     static var onceToken: dispatch_once_t = 0
     static var instance: Singleton? = nil
     }
     dispatch_once(&Static.onceToken) {
     Static.instance = Singleton()
     }
     return Static.instance!
     }
     }
    https://stackoverflow.com/questions/24024549/using-a-dispatch-once-singleton-model-in-swift/24147830#24147830
     */

    2 代理的具体代码

    ViewController:

    import UIKit
    
    class ViewController: UIViewController, dwDelegate { // 第四步: 遵守协议
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.white
            makeBtn()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        func makeBtn() {
            let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
            btn.setTitle("btn", for: UIControlState.normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
            self.view.addSubview(btn)
        }
        
        @objc func btnClick(){
        print("ddd")
            let vc = SecondViewController.init()
            vc.delegate = self // 第五步,遵守协议
            present(vc, animated: true, completion: nil)
        }
        
        func changeString(newString: String) { // 第六步: 实现协议
            print(newString)
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }

    SecondViewController:

    import UIKit
    
    protocol dwDelegate {//第一步 定义协议
        func changeString(newString: String)
    }
    
    class SecondViewController: UIViewController {
    
        var delegate: dwDelegate?  // 第二步  声明协议
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.lightGray
            makeBtn()
            // Do any additional setup after loading the view.
        }
        
        func makeBtn() {
            let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
            btn.setTitle("返回", for: UIControlState.normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
            self.view.addSubview(btn)
        }
        
        @objc func btnClick(){
            print("ddd")
            dismiss(animated: true) {
                print("1")
                self.delegate!.changeString(newString: "hahaha") // 第三步 触发协议
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    3 闭包的具体代码

    ViewController

    import UIKit
    
    class ViewController: UIViewController {
        
        var cusVC = CustomPickerView()
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.brown
            //makePickerView()
            makeNewView()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        func makeNewView() {
            cusVC = CustomPickerView.init(frame: CGRect(x: 0,y: UIScreen.main.bounds.size.height - 0,width: UIScreen.main.bounds.size.width, height: 260))
            self.view.addSubview(cusVC)
            cusVC.callBackBlock { (str) in  //第五步 触发闭包回调
                print(str)
            }
        }
        
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            UIView .animate(withDuration: 0.5) {
                self.cusVC.frame = CGRect(x: 0,y: UIScreen.main.bounds.size.height - 260,width: UIScreen.main.bounds.size.width, height: 260)
            };
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }

    CustomPickerView:

    import UIKit
    //第一步 定义闭包
    typealias swiftBlock = (_ str: String) -> Void
    
    class CustomPickerView: UIView {
    
        var callBack : swiftBlock? //第二步 声明
        
        override init(frame:CGRect) {
            super.init(frame:frame)
            self.backgroundColor = UIColor.white
            makeTwoBtn()
        }
        
        func callBackBlock(_ block: @escaping swiftBlock) { // 第三步 实现闭包
            callBack = block
        }
        
        @objc func twoBtnClick(btn: UIButton) {
            UIView.animate(withDuration: 0.1, animations: {
                self.frame = CGRect(x: 0,y: UIScreen.main.bounds.size.height - 0,width: UIScreen.main.bounds.size.width, height: 260)
            })
            
            if callBack != nil {  // 第四步 触发闭包
                callBack!("闭包传值")
            }
        }
        
        func makeTwoBtn() {
            let leftBtn = UIButton(frame:CGRect(x: 60,y: 105,width: 50, height:30))
            leftBtn.setTitle("确定", for: UIControlState.normal)
            leftBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            leftBtn.layer.cornerRadius = 2
            leftBtn.layer.masksToBounds = true
            leftBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 100/255.0, blue: 150/255.0, alpha: 1)
            leftBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
            leftBtn.addTarget(self, action: #selector(twoBtnClick), for: UIControlEvents.touchUpInside)
            self.addSubview(leftBtn)
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }

     

    展开全文
  • swift-传值

    2018-04-02 20:22:09
    //2个界面传值 界面1 /传值 //需求2个类传值传值有通知,属性,delegate,闭包; import UIKit //遵守代理 class ViewController: UIViewController,TwoMessageDelegate { let textlable = UILabel(); let TwoVc...

    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: ["" : ""])
        }
    
    展开全文
  • Swift传值

    2016-06-27 13:40:20
    Swift 传值

    源代码下载地址:http://download.csdn.net/detail/u012450066/9559017
    建议下载源代码,更容易理解一点。

    1. 属性传值

    值是从第一个界面传给第二个界面

    第一个界面

        /**
         属性传值
    
         - parameter indexPath: indexPath
         */
        func propertyTransferValue(indexPath: NSIndexPath) {
            let vc = SecondViewController()
            vc.value = datas[indexPath.row]
            vc.someArrays = datas
            navigationController?.pushViewController(vc, animated: true)
        }

    第二个界面,接收值

    class SecondViewController: UIViewController {
    
        var someArrays = [String]()
    
        var value : String?
    
        @IBOutlet weak var transferLabelValue: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.title = value
            transferLabelValue.text = value
            print(someArrays)
        }
    
    }

    2.代理传值

    值是从第二个界面传回第一个界面

    第一个界面

        /**
         代理传值
    
         - parameter indexPath: indexPath
         */
        func protocolTransferValue(indexPath: NSIndexPath) {
            let vc = ProtocolViewController()
            vc.value = datas[indexPath.row]
            vc.delegate = self;
            navigationController?.pushViewController(vc, animated: true)
        }
    
        /**
         ProtocolViewControllerDelegate
    
        要遵守ProtocolViewControllerDelegate协议, 以“ , ”分开,没有OC中的<>
    
        class FirstViewController: UIViewController, UITableViewDelegate,     UITableViewDataSource, ProtocolViewControllerDelegate{}
         - parameter value: 代理传回来的值
         */
        func protocolTransfer(value: String) {
            datas[index.row] = value
            tableView.reloadData()
        }

    第二个界面

    protocol ProtocolViewControllerDelegate {
        func protocolTransfer(value: String)
    }
    
    class ProtocolViewController: UIViewController {
    
        @IBOutlet weak var protocolLabel: UILabel!
        @IBOutlet weak var protocolButton: UIButton!
    
        var value : String?
        //   =nil
        var delegate : ProtocolViewControllerDelegate? = nil
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.title = value
    
        }
    
        @IBAction func protocolButtonAction(sender: UIButton) {
            delegate?.protocolTransfer(protocolLabel.text!)
            navigationController?.popViewControllerAnimated(true)
        }
    }

    3.闭包传值(oc中的代码块)

    值是从第二个界面传回第一个界面

    第一个界面

        /**
         闭包
    
         - parameter indexPath: indexPath
         */
        func closureTransferValue(indexPath: NSIndexPath) {
            let vc = ClosureViewController()
            vc.clousureVoid = { () -> Void in
                print("回调过来了")
            }
    
        vc.clousureValue = { (text : String) -> Void in
                print("回调传回来的值 = \(text)");
            }
            navigationController?.showViewController(vc, sender: nil)
        }

    第二个界面

    import UIKit
    
    typealias clousureVoidType = () -> Void
    typealias clousureValueType   = (text : String) -> Void
    
    class ClosureViewController: UIViewController {
    
        var clousureVoid : clousureVoidType?
        var clousureValue : clousureValueType?
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.whiteColor()
            /** 只是一个回调 */
            clousureVoid!();
    
            let btn = UIButton.init(type:.Custom)
            btn.frame = CGRectMake(0, 0,100, 50)
            btn.center = view.center;
            btn.backgroundColor = UIColor.cyanColor()
            btn.setTitle("闭包传值", forState: .Normal)
            btn.addTarget(self, action: #selector(ClosureViewController.btnAction), forControlEvents: .TouchUpInside)
            view.addSubview(btn)
        }
    
       func btnAction() {
            /** 传值 */
            clousureValue!(text: "ClosureViewController")
            navigationController?.popViewControllerAnimated(true)
        }
    
    }

    4.通知传值

    值是从第二个界面传回第一个界面

    第一个界面

     // MARK: - 添加通知
        func addNotifacetion () {
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FirstViewController.notifacetionTransferAction(_:)), name: "notifacetionTransfer", object: nil)
        }
    
        /** 接收到通知后执行方法 */
        func notifacetionTransferAction(notifacetion : NSNotification) {
            print("\(notifacetion.userInfo)")
        }
    
        // MARK: - deinit
        /** 移除通知 */
        deinit {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }

    第二个界面

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            NSNotificationCenter.defaultCenter().postNotificationName("notifacetionTransfer", object: self, userInfo: ["key":"value"])
            navigationController?.popViewControllerAnimated(true)
    
        }
    展开全文
  • swift-iOS五种传值方法

    2020-07-23 23:30:23
    iOS五种传值方法
  • Swift 传值之属性传值

    千次阅读 2016-01-29 16:39:57
    Swift 的语言非常简练,这就让它的属性传值功能变得更加强大,从第一个界面传到第二个界面,这种方法非常简单,这里就不在赘述。下面这个例子说的是用属性传值从第二个控制器传值到第一个控制器的方法。 首先,在...

    Swift 的语言非常简练,这就让它的属性传值功能变得更加强大,从第一个界面传到第二个界面,这种方法非常简单,这里就不在赘述。下面这个例子说的是用属性传值从第二个控制器传值到第一个控制器的方法。

    属性传值:http://blog.csdn.net/zhangjitao_boke/article/details/50606185

    代理传值:http://blog.csdn.net/zhangjitao_boke/article/details/50606707

    闭包传值:http://blog.csdn.net/zhangjitao_boke/article/details/50607325

    通知传值:http://blog.csdn.net/zhangjitao_boke/article/details/50607818

    单例传值:http://blog.csdn.net/zhangjitao_boke/article/details/50608066



    首先,在storyBoard里面拖如下界面,从左到右分别是导航控制器,FirstViewController,SecondViewController


    创建好界面,后创建相应的控制器,并且关联控制器和控制器上的textField,顺便添加一下StoryBoard ID


    项目目录如下



    方法简单,几行代码实现,现在直接上代码


    在FirstViewController实现代码如下


    //
    // FirstViewController.swift
    // ShuxingPassValue
    //
    // Created by JT on 16/1/29.
    // Copyright © 2016年 JT. All rights reserved.
    //

    import UIKit

    class FirstViewController: UIViewController {

    @IBOutlet weak var firstTextField: UITextField!

    override func viewDidLoad() {
    super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }
    // 这里用最简单的方法
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
    let secondVC = storyBoard.instantiateViewControllerWithIdentifier("secondVC") as! SecondViewController

    // 这里是将FirstViewController传递给下个界面以供使用
    secondVC.firstVC = self
    self.navigationController?.pushViewController(secondVC, animated: true)

    }

    }


    在SecondViewController实现代码如下(设置 firstVC变量来接收FirstViewController)

    //
    // SecondViewController.swift
    // ShuxingPassValue
    //
    // Created by JT on 16/1/29.
    // Copyright © 2016年 JT. All rights reserved.
    //

    import UIKit

    class SecondViewController: UIViewController {

    @IBOutlet weak var secondTextField: UITextField!


    var firstVC:FirstViewController!


    override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
    }

    func backAction() {
    // 传值
    firstVC.firstTextField.text = secondTextField.text

    self.navigationController?.popToRootViewControllerAnimated(true)

    }


    }


    实现完上面的部分我们的属性逆向传值就完成了,现在看下效果


    第二个控制器


    第一个控制器效果


    swift属性传值这里就叙述完毕了



    展开全文
  • Swift实现传值的四种方法

    千次阅读 2016-07-02 13:20:14
    常用的四种传值方法:单例,代理,闭包,通知(一) (例如:从页面a,反向传值(这里是背景颜色backgroundColor)到页面b) 1.单例 1>.创建单例变量 在AppDelegate.swift 中创建变量var backgroundColor:UIColor? 2>在页面...
  • OC、swift混编中的相互跳转和传值

    千次阅读 2016-10-28 11:40:11
    OC、swift混编中的相互跳转和传值
  • Swift Block传值

    千次阅读 2017-03-02 11:10:53
    Swift3.0来实现传值-->前后两个控制器的反向传值,这里传递了一个地址的model 第一个类 let address = MZAddressManagerViewController.init() address.orderInfo = self.orderInfo //将当前...
  • Swift 传值之闭包传值

    千次阅读 2016-01-29 16:37:31
    Swift的闭包传值和OC中的block传值非常相似 项目的建立和之前的属性传值和代理传值类似  属性传值:http://blog.csdn.net/zhangjitao_boke/article/details/50606185 代理传值:...
  • Swift 闭包(block)传值

    千次阅读 2016-04-30 12:17:45
    前面写了Swift的通知和代理的传值: 《Swift NSNotificationCenter的使用》 《Swift 协议代理的使用和传值》 这篇就介绍关于闭包传值的使用。在iOS常用的三种反向传值方式也就基本介绍完了。效果图具体使用步骤...
  • UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 150, 150, 150)];  imageView.image = [UIImage imageNamed:@"11"];    imageView.userInteractionEnabled = ...YE
  • 之前我们做过如果要点击一个按钮实现跳转到另一个按钮的办法,有时候我们需要通过一些动作来触发页面的跳转,比如摇动手机,这样就不能直接用连线的办法来实现跳转了,那么如何通过代码的方式来实现跳转呢?...
  • Swift 闭包(Closure)回调传值

    千次阅读 2015-08-11 16:55:37
    B - > A 使用闭包进行反向回调传值Swift 使用闭包(Closure)传值的原理,与OC 中使用代码块(block)传值原理,基本类似按步骤可以如下理解: 1、定义闭包。 2、闭包赋值(传送) 3、闭包调用。至于定义闭包应该...
  • Swift语言, 我是折叠tableview,自定义了一个cell,cell里面有很多按钮,点击每个按钮都跳转到不同的页面,我想请教下如何带值跳转呢?
  • iOS storyboard反向传值

    2015-07-07 13:56:48
    iOS swift storyboard 反向传值,A是主界面,B是子界面,现在A跳转到B,B跳回A已经实现。A传值给B 也成功了,该如何实现B传值给A 呢? 用swift语言
  • swift3.0 传值总结(属性、代理、闭包、通知)
  • swift开发使用闭包传值

    千次阅读 2016-05-06 15:25:18
    swift开发使用闭包传值开发中,从一个页面跳转到另一个页面的时候,有时候需要到跳转后的页面把值传回上一个页面,这里介绍一下swift开发中使用闭包传值 首先我们创建两个视图控制器分别为”ViewController“和”...
  • - (void) goBackToPersonInfoVCWithNickName:(NSString *) nickName{ // [self.navigationController popViewControllerAnimated:YES]; //此页面已经存在于self.navigationController.viewControllers中,并
  • iOS Selector传值的常见方法

    千次阅读 2016-05-26 15:34:13
    selector常见的两种传值方法,一种是通过UIButton的tag属性,另一种是通过NSTimer的userInfo;话不多说,直接上Demo截图。因为想把截图做小一点,写得有点紧凑了。
  • ios swift开发中有几种方式传值,看到简书上一篇不错的文章。 链接:http://www.jianshu.com/p/3e1173652996一.通过segue进行传值二.通过delegate进行传值三.通过Notification进行传值四.通过回调函数进行传值需求...
1 2 3 4 5 ... 20
收藏数 2,023
精华内容 809
关键字:

swift传值