swift find window, simply use the navigation bar, Controller values pass through the closure simple example

 

 Here are two of the Controller I need to jump from FirstVC TwoVC, when TwoVC return pass parameters to FirstVC

 

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

 

  • FirstVC jump codes
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 return code
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)
    }

}

 

Guess you like

Origin www.cnblogs.com/hualuoshuijia/p/11655712.html