swift找window,导航栏的简单使用,Controller通过闭包传值简单示例
Posted hualuoshuijia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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) } }
以上是关于swift找window,导航栏的简单使用,Controller通过闭包传值简单示例的主要内容,如果未能解决你的问题,请参考以下文章