美文网首页
iOS Swift 跳转传值

iOS Swift 跳转传值

作者: 已经多年不写Unity | 来源:发表于2018-12-25 11:06 被阅读0次

建立项目,拖拉拽如下:

a.png
页面1 ViewController.swift 中代码如下:
//
//  ViewController.swift
//  Nav
//
//  Created by Wanghui on 2018/12/24.
//  Copyright © 2018 Wanghui. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @IBAction func test(_ sender: Any) {
        print("testBtn")
        self.performSegue(withIdentifier: "login", sender: self)
    }
    
    
    //重写这个方法 准备跳转时候调用
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "my01"{
                     let controller = segue.destination as! MyViewController
                                    controller.deviceUdid = "wanghui"
        }
        else if(segue.identifier == "login")
        {
               let controller = segue.destination as! MyViewController
                                    controller.deviceUdid = "Hello,World!"
        }
        
    }

}


接受页面 MyViewController.swift 代码如下:

//  MyViewController.swift
//  Nav
//
//  Created by Wanghui on 2018/12/24.
//  Copyright © 2018 Wanghui. All rights reserved.
//

import UIKit

class MyViewController: UIViewController {

    var deviceUdid:String = "" ;
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        print("得到了消息!:\(deviceUdid)")
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

调试结果如下:
testBtn
得到了消息!:Hello,World!

相关文章

网友评论

      本文标题:iOS Swift 跳转传值

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