Swift/Storyboard 界面跳转/传值

跳转

Push时的Controller为 UIViewController
Present时的Controller为 UINavigationController

  • 使用 Storyboard Segue 方式:
    在Storyboard中设置Segue的Identifier,以及Kind
    然后使用代码:
performSegue(withIdentifier: "LoginToMain", sender: nil)

  • 使用代码方式:
let storyboard = UIStoryboard(name: "Segue", bundle: nil)
// Push
let viewController = storyboard.instantiateViewController(withIdentifier: "NavigationController")
navigationController?.pushViewController(viewController, animated: true)
// Present
let navController = storyboard.instantiateViewController(withIdentifier: "ViewController")
navigationController?.present(navController, animated: true, completion: nil)

传值

目标是 UINavigationViewControlle r的话,得先取出TopViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Segue" {
        if let nav = segue.destination as? UINavigationController, let controller = nav.topViewController as? ViewController {
            controller.intentData = "aaa"
        }
    }
}

否则:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Segue", let c = segue.destination as? ViewController {
        controller.intentData = sender as? String // sender 为 performSegue 方法设置的值
    }
}


你可能感兴趣的:(Swift/Storyboard 界面跳转/传值)