我将用户发送到一个页面上的按钮点击。这个页面是一个UITableViewController。
现在如果用户点击一个单元格,我想把他推回到上一页。
我想到了self.performSegue("back")....但这似乎不是一个好主意。
正确的做法是什么?
我将用户发送到一个页面上的按钮点击。这个页面是一个UITableViewController。
现在如果用户点击一个单元格,我想把他推回到上一页。
我想到了self.performSegue("back")....但这似乎不是一个好主意。
正确的做法是什么?
当前回答
这个很适合我(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")
}
}
}
}
其他回答
斯威夫特3:
如果你想回到之前的视图控制器
_ = navigationController?.popViewController(animated: true)
如果你想回到根视图控制器
_ = navigationController?.popToRootViewController(animated: true)
如果你没有使用导航控制器,请使用下面的代码。
self.dismiss(animated: true, completion: nil)
动画值可以根据您的要求设置。
在这种情况下,你从UIViewController中呈现一个UIViewController,也就是。
// Main View Controller
self.present(otherViewController, animated: true)
简单地调用dismiss函数:
// Other View Controller
self.dismiss(animated: true)
这个很适合我(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")
}
}
}
}
试试这个: 对于前面的视图使用这个:
navigationController?.popViewController(animated: true)
弹出到根目录使用以下代码:
navigationController?.popToRootViewController(animated: true)
斯威夫特3,斯威夫特4
if movetoroot {
navigationController?.popToRootViewController(animated: true)
} else {
navigationController?.popViewController(animated: true)
}
navigationController是可选的,因为可能没有。