首页 > 解决方案 > 使用 viewController 的 performSegue 线程错误?iOS

问题描述

在我授权用户的 TouchID 后,我试图跳转到第二个视图控制器。我能够验证 TouchID 是否正常工作,但我遇到了跳转到第二个 viewController 的问题。

我创建了一个 SecondViewController 和一个带有标识符“dispenseScreen”的 Segue。但是,每当我尝试跳转到第二个屏幕时,我的程序就会崩溃。

@IBAction func touchID(_ sender: Any)
    {

        let context:LAContext = LAContext()

        //Removes Enter Password during failed TouchID
        context.localizedFallbackTitle = ""

        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
        {
                context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "We require your TouchID", reply: { (wasCorrect, error) in

                self.isBiometryReady()
                if wasCorrect {
                    self.performSegue(withIdentifier: "dispenseScreen", sender: self)
                    print("Correct")
                }
                else {
                    print("Incorrect")
                }
            })
        } else {
            //Enter phone password if too many login attempts
            //Add message alerting user that TouchID is not enabled
        }

    }

我的代码中没有语义错误,但是当我尝试转到第二个视图控制器时收到线程错误。

标签: iosswiftxcodeviewcontroller

解决方案


您正在尝试在evaluatePolicy. 对于涉及 UI 的任何事情,您需要确保您在主线程上:(wasCorrect, error) in DispatchQueue.main.async { ... }


推荐阅读