当前视图控制器nil键错误

时间:2017-11-01 22:32:22

标签: ios swift

如果登录凭证已经过验证,我试图让我的应用程序只是屏幕,但由于某种原因,在segue上有一个致命的错误。以下是我遇到的错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKeyedSubscript:]: key cannot be nil'
***

下面是我正在运行的代码:

if user!.isEmailVerified {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
    self.present(vc, animated: true, completion: nil)                    
}

segue打印前的print语句,而不是之后的打印语句。

3 个答案:

答案 0 :(得分:0)

如果视图控制器位于同一个故事板上,那么创建视图控制器实例并显示它的方式是正确的。问题发生在其他地方,可能是异步的。确保isEmailVerified按预期工作。

答案 1 :(得分:0)

可能的解决方案:

  1. 检查用户是否存在:guard let user = User, user.isEmailVerified else {return}
  2. 2.使用app delegate而不是storyboard。

    guard let rootController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ProfileView") as? ProfileView else {return}
    self.present(rootController, animated: true, completion: nil)
    

答案 2 :(得分:0)

试试这个:

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "ProfileView")
    self.present(controller, animated: true, completion: nil)

转到故事板并选择您的ProfileView控制器。然后,在Identity Inspector中确认您已为该View Controller选择了ProfileView。在我下面的例子中,我已经将ActionViewController子类化了。

enter image description here