swift找window,导航栏的简单使用,Controller通过闭包传值简单示例
这里是我需要的两个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) } }