我将用户发送到一个页面上的按钮点击。这个页面是一个UITableViewController。
现在如果用户点击一个单元格,我想把他推回到上一页。
我想到了self.performSegue("back")....但这似乎不是一个好主意。
正确的做法是什么?
我将用户发送到一个页面上的按钮点击。这个页面是一个UITableViewController。
现在如果用户点击一个单元格,我想把他推回到上一页。
我想到了self.performSegue("back")....但这似乎不是一个好主意。
正确的做法是什么?
当前回答
斯威夫特3,斯威夫特4
if movetoroot {
navigationController?.popToRootViewController(animated: true)
} else {
navigationController?.popViewController(animated: true)
}
navigationController是可选的,因为可能没有。
其他回答
Swift 4.0 Xcode 10.0与TabViewController作为最后一个视图
如果你的上一个ViewController是嵌入在一个TabViewController中,下面的代码将把你发送到根…
navigationController?.popToRootViewController(animated: true)
navigationController?.popViewController(animated: true)
但如果你真的想回到上一个视图(可能是Tab1, Tab2或Tab3视图..),你必须写下面的代码:
_ = self.navigationController?.popViewController(animated: true)
这对我来说是有效的,我在我的TabView之后使用了一个视图:)
我可以通过在导航控制器的“viewDidDisappear”中写代码重定向到根页面,
override func viewDidDisappear(_ animated: Bool) { self.navigationController ?。popToRootViewController(动画:真) }
试试这个: 对于前面的视图使用这个:
navigationController?.popViewController(animated: true)
弹出到根目录使用以下代码:
navigationController?.popToRootViewController(animated: true)
如果你想关闭前两个视图控制器,只需像这样调用popViewController两次:
self.navigationController?.popViewController(animated: true)
self.navigationController?.popViewController(animated: true)
关于如何在故事板中嵌入viewController到navigationController的问题:
打开不同viewController所在的故事板 点击你想要导航控制器开始的viewController 在Xcode顶部,点击“编辑器” ->轻按“嵌入” ->轻按“导航控制器”