我正在开发一个 ForgotPassword 按钮,这没问题,如果我单击它,它会调用 PerformSegueWithIdentifier 并打开一个新的 ViewController。现在,如果凭据错误(密码错误),我会在登录时收到警报,并且我添加了一个请求密码的按钮来调用 ForgotPassword。
问题是它正确调用了AlertAction,这调用了forgetPassword IBAction,并且performSegue也被正确调用,但 View 不会出现。
// MARK: Actions
private func forgotPasswordAlertAction(action: UIAlertAction) {
print("AlertRequestAction")
forgotPassword(action)
}
@IBAction func forgotPassword(sender: AnyObject) {
print("forgotPasswortAction")
print(self)
performSegueWithIdentifier(forgotPasswordId, sender: self)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == forgotPasswordId,
let controller = segue.destinationViewController as? AccountForgotPasswordViewController {
print("forgotPasswortSegue")
controller.emailFromInput = emailTextField.text
}
}
private func showError(error: AuthenticationError) {
let title = NSLocalizedString("Error", comment: "Generic Title if an unkown error occured.")
let message = NSLocalizedString("An Error occured. Please try again.", comment: "Generic Message if an unkown error occured.")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let dismissAction = UIAlertAction(title: NSLocalizedString("OK", comment: "Generic OK Button label"), style: .Default, handler: nil)
switch error {
case .BadCredentials:
alertController.title = NSLocalizedString("Invalid Credentials", comment: "Title if provided credentials are invalid.")
alertController.message = NSLocalizedString(
"The entered credentials are invalid. Please try again with valid credentials or request a new password.",
comment: "Generic Title if an unkown error occured.")
let forgotPasswordAction = UIAlertAction(
title: NSLocalizedString("Request Password", comment: "Request Password Button Label"), style: .Default, handler: forgotPasswordAlertAction)
alertController.addAction(forgotPasswordAction)
default:
break
}
alertController.addAction(dismissAction)
alertController.preferredAction = dismissAction
presentViewController(alertController, animated: true, completion: nil)
}
最佳答案
在你的情况下,我能想象到的唯一问题是,当你被调用警报时,你会以某种方式从主队列中移出。通过在 forgotPasswordAlertAction(_:)
函数中添加行 print(NSThread.isMainThread()
) 来确认这一点。如果您将在控制台中看到 false
,然后调度到主队列:
private func forgotPasswordAlertAction(action: UIAlertAction) {
print("AlertRequestAction")
print(NSThread.isMainThread())
dispatch_async(dispatch_get_main_queue()) {
self.forgotPassword(action)
}
}
关于ios - 快速警报操作不适用于 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37613174/