ios - 快速警报操作不适用于 segue

标签 ios swift navigation segue alert

我正在开发一个 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
    }
}

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

最佳答案

在你的情况下,我能想象到的唯一问题是,当你被调用警报时,你会以某种方式从主队列中移出。通过在 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/

相关文章:

ios - _UIRemoteView - 在 Xcode 中显示 View 层次结构

ios - 有没有办法只旋转ios中按钮的文本

swift - 在 Swift 中使用索引映射或减少

ios - 如何在单击下一个按钮时播放下一首歌曲

ios - xcode/ Storyboard : can't drag bar button to toolbar at top

ios - NSBundle pathForResource :ofType: and UIImage imageWithContentsOfFile: handle scale and device modifiers? 怎么办

ios - AWS S3 iOS - 了解列出对象何时完成

ios - 如何在旧版本中创建Objective-C项目

java - 具有多个 Activity 的抽屉导航。 - 空指针异常

javascript - 如何将事件 CSS block 导航元素(跨度)设置为与使用 Javascript 和 "active"类不同的颜色?