码迷,mamicode.com
首页 > 编程语言 > 详细

swift找window,导航栏的简单使用,Controller通过闭包传值简单示例

时间:2019-10-11 17:55:13      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:bsp   touch   sel   导航   message   导航栏   info   navig   red   

技术图片

 

 这里是我需要的两个Controller,从FirstVC跳转到TwoVC,TwoVC返回的时候给FirstVC传递参数

 

  • 找window
let vc = UINavigationController.init(rootViewController:  FirstVC())
 let rootVC = UIApplication.shared.delegate as! AppDelegate
 rootVC.window?.rootViewController = vc

 

  • FirstVC的跳转代码
class FirstVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "first"
        self.view.backgroundColor = UIColor.red
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let twoVC = TwoVC();
        twoVC.two = "这是给第二个页面传递的参数"
        twoVC.block = {(message:String)->(Void) in
            print("FirstVC---------------\(message)")
        }
        
        self.navigationController?.pushViewController(twoVC, animated:true)
    }

}

 

 

  • TwoVC的返回代码
typealias Block = (_ message:String)->(Void)

class TwoVC: UIViewController {

    var two :String?
    var block :Block?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "two"
        self.view.backgroundColor = UIColor.blue
        print("TwoVC-------\(self.two)")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if((self.block) != nil){
            self.block!("这里是给第一个页面传递的参数")
        }
        self.navigationController?.popViewController(animated: true)
    }

}

 

swift找window,导航栏的简单使用,Controller通过闭包传值简单示例

标签:bsp   touch   sel   导航   message   导航栏   info   navig   red   

原文地址:https://www.cnblogs.com/hualuoshuijia/p/11655712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!