如何在执行导航 segue 之前检查条件
How to check conditions before performing navigation segue
我有一个故事板控制的应用程序。在注册页面上,我想在按下注册按钮时将用户发送到主页。所以我从按钮拖了一个 segue 到主页。但是在执行 segue 之前我无法检查条件。但是如果我创建一个 segue 并以编程方式执行它,主页就会出现在注册页面上,允许用户向后滑动。有人可以告诉我如何在执行 segue 之前检查条件,或者如果以编程方式执行此操作,则不允许用户返回注册页面。这是我的故事板。 Main.storyboard
- 重新连接从 控制器 到目标的 segue。
- 将按钮连接到 IBAction。
- 在 IBAction 中 检查条件 并在成功时调用
performSegue(withIdentifier:sender:)
。
Xcode 12.0 Swift 5.0
- 首先,您需要将“注册”按钮连接到代码中的 IBAction;
- 在 IBAction 中你可以调用函数:
func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
.
- 如果在条件下一切都成功,则调用此函数,否则
return
出错。
退出按钮示例:
private func showLoginViewController() {
// Creates the view controller with the specified identifier
let vc = storyboard?.instantiateViewController(withIdentifier: "loginForm") as! LoginViewController
let navigationVC = UINavigationController(rootViewController: vc)
navigationVC.modalPresentationStyle = .fullScreen
present(navigationVC, animated: true, completion: nil)
}
// Tap on button
@IBAction func signOutUserButton(_: UIButton) {
let alertController = UIAlertController(title: nil, message: "Are you sure you want to sign out?", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Sign Out", style: .destructive, handler: { _ in
// Condition where I check possibility to sign out
self.signOut()
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true)
}
private func signOut() {
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
// If everything is okay then perform segue
showLoginViewController()
} catch let signOutError as NSError {
// Otherwise show error
print("Error signing out: %@", signOutError)
}
}
我有一个故事板控制的应用程序。在注册页面上,我想在按下注册按钮时将用户发送到主页。所以我从按钮拖了一个 segue 到主页。但是在执行 segue 之前我无法检查条件。但是如果我创建一个 segue 并以编程方式执行它,主页就会出现在注册页面上,允许用户向后滑动。有人可以告诉我如何在执行 segue 之前检查条件,或者如果以编程方式执行此操作,则不允许用户返回注册页面。这是我的故事板。 Main.storyboard
- 重新连接从 控制器 到目标的 segue。
- 将按钮连接到 IBAction。
- 在 IBAction 中 检查条件 并在成功时调用
performSegue(withIdentifier:sender:)
。
Xcode 12.0 Swift 5.0
- 首先,您需要将“注册”按钮连接到代码中的 IBAction;
- 在 IBAction 中你可以调用函数:
func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
. - 如果在条件下一切都成功,则调用此函数,否则
return
出错。
退出按钮示例:
private func showLoginViewController() {
// Creates the view controller with the specified identifier
let vc = storyboard?.instantiateViewController(withIdentifier: "loginForm") as! LoginViewController
let navigationVC = UINavigationController(rootViewController: vc)
navigationVC.modalPresentationStyle = .fullScreen
present(navigationVC, animated: true, completion: nil)
}
// Tap on button
@IBAction func signOutUserButton(_: UIButton) {
let alertController = UIAlertController(title: nil, message: "Are you sure you want to sign out?", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Sign Out", style: .destructive, handler: { _ in
// Condition where I check possibility to sign out
self.signOut()
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true)
}
private func signOut() {
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
// If everything is okay then perform segue
showLoginViewController()
} catch let signOutError as NSError {
// Otherwise show error
print("Error signing out: %@", signOutError)
}
}