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 方法设置的值
    }
}



作者:竹菜板
链接:https://www.jianshu.com/p/533797f9ff86
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Swift/Storyboard 界面跳转/传值

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页