Viewcontroller在被另一个viewcontroller呈现后崩溃

时间:2017-07-05 11:44:41

标签: ios swift

出于某种原因,class ReplaceSegue: UIStoryboardSegue { override func perform() { let storyBoard = UIStoryboard(name: "Main", bundle: nil) let newRootView = storyBoard.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController sourceViewController.navigationController?.setViewControllers([newRootView], animated: true) } }

崩溃
viewDidLoad()

在我的应用中的fatal error: unexpectedly found nil while unwrapping an Optional value 中设置用户界面后,从另一个视图控制器中显示:

authViewController

其他视图控制器:

//authViewController
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        authState.addTarget(self, action: #selector(authStateChanged), for: .valueChanged)//<-- The place where it crashes

        emailInput.addTarget(self, action: #selector(emailPasswordTextChanged), for: .editingChanged)
        passwordInput.addTarget(self, action: #selector(emailPasswordTextChanged), for: .editingChanged)
        SignIn.addTarget(self, action: #selector(signInPressed), for: .touchUpInside)
        //Set actions

        infoView.layer.cornerRadius = 5
        SignIn.layer.cornerRadius = 5
        //UI setup
}

如果你知道,有人可以告诉我它发生了什么以及为什么会发生?

2 个答案:

答案 0 :(得分:6)

而不是像ViewController那样展示

self.present(authViewController(), animated: true, completion: nil)

在实例化后尝试展示ViewController

let storyBoard = UIStoryboard(name: storyboardname, bundle: nil)
let authVC = storyBoard.instantiateViewController(withIdentifier: viewController Identifier) as? authViewController
self.present(authVC, animated: true, completion: nil)

答案 1 :(得分:0)

我解决了这个问题:

let authVC = self.storyboard?.instantiateViewController(withIdentifier: "authVC")
self.present(authVC!, animated: true, completion: nil)
相关问题