我将用户发送到一个页面上的按钮点击。这个页面是一个UITableViewController。
现在如果用户点击一个单元格,我想把他推回到上一页。
我想到了self.performSegue("back")....但这似乎不是一个好主意。
正确的做法是什么?
我将用户发送到一个页面上的按钮点击。这个页面是一个UITableViewController。
现在如果用户点击一个单元格,我想把他推回到上一页。
我想到了self.performSegue("back")....但这似乎不是一个好主意。
正确的做法是什么?
当前回答
如果Segue是“Show”或“Push”类型,那么你可以在UINavigationController的实例上调用“popViewController(animated: Bool)”。或者如果segue是present,那么调用UIViewController的实例"dismiss(animated: Bool, completion: (() -> Void)?
其他回答
斯威夫特4
有两种方法返回/返回之前的ViewController:
第一种情况:如果你使用:self.navigationController?。pushViewController(yourViewController, animated: true) 在这种情况下,你需要使用self.navigationController?会被(动画:真) 第二种情况:如果你使用:self。present(yourViewController, animated: true, completion: nil) 在这种情况下,您需要使用self。解散(动画:true,完成:nil)
在第一种情况下,确保你在故事板中嵌入了ViewController到navigationController
这个很适合我(Swift UI)
struct DetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
VStack {
Text("This is the detail view")
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Back")
}
}
}
}
我可以通过在导航控制器的“viewDidDisappear”中写代码重定向到根页面,
override func viewDidDisappear(_ animated: Bool) { self.navigationController ?。popToRootViewController(动画:真) }
Swift 5及以上
案例1:与导航控制器一起使用
回到之前的视图控制器 self.navigationController ?。会被(动画:真) 回到根视图控制器 self.navigationController ?。popToRootViewController(动画:真)
案例2:使用present视图控制器
self.dismiss(animated: true, completion: nil)
如果Segue是“Show”或“Push”类型,那么你可以在UINavigationController的实例上调用“popViewController(animated: Bool)”。或者如果segue是present,那么调用UIViewController的实例"dismiss(animated: Bool, completion: (() -> Void)?