【问题标题】:Swift Alert Action not working with segueSwift Alert Action 不适用于 segue
【发布时间】:2016-06-03 11:26:53
【问题描述】:

我正在开发一个忘记密码的按钮,这没问题,如果我点击它,它会调用 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
    }
}

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

【问题讨论】:

  • 你一定会得到一些关于在你的控制台上打印动画的声明,不是吗???
  • 这是我在控制台上得到的:
  • AlertRequestAction forgotPasswortAction forgotPasswortSegue
  • 你能确认你的segues在storyboard中的边界是正确的吗???
  • 是的,它适用于正常的 forgotPasswordButton

标签: ios swift navigation segue alert


【解决方案1】:

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

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

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

【讨论】:

  • 当您找到答案时,请为我们发布答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
相关资源
最近更新 更多