Swift Alert Action不使用segue

时间:2016-06-03 11:26:53

标签: ios swift navigation segue alert

我正在使用ForgotPassword按钮这没问题,如果我点击它,它会调用performSegueWithIdentifier并打开一个新的ViewController。现在,如果凭据错误(密码错误),我会在登录时发出警报,并且我添加了一个按钮来请求密码来调用ForgotPassword。

问题是它调用AlertAction是否正确,这会调用forgotPassword IBAction并且还正确调用performSegue,但视图不会出现。

// 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
    }
}

Segue from LoginController to ForgotPasswordController

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)
}

1 个答案:

答案 0 :(得分:0)

我能想象到的唯一问题 - 当你被叫做警报时,你会以某种方式从主队列中移出。通过在print(NSThread.isMainThread()功能中添加第forgotPasswordAlertAction(_:)行来确认这一点。如果您在控制台中看到false,则调度到主队列:

private func forgotPasswordAlertAction(action: UIAlertAction) {
    print("AlertRequestAction")
    print(NSThread.isMainThread())

    dispatch_async(dispatch_get_main_queue()) { 
        self.forgotPassword(action)
    }
}