ios - fatal error : unexpectedly found nil while unwrapping an Optional value on performing segue

标签 ios swift segue facebook-sdk-4.0

我正在使用 facebook SDK 登录,一切都已正确设置。当我第一次授权我的应用程序时,segue 将执行到我的下一个 View Controller 。但是当我注销并尝试再次登录时,segue 出现错误

fatal error :在解包可选值时意外发现 nil

这是我的 appDelegate 文件中的代码

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool{

    let result = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)

    if result {

        let token = FBSDKAccessToken.currentAccessToken()
        let fieldsMapping = [
            "id" : "facebookId",
            "name" : "name",
            "email": "email"
        ]

        backendless.userService.loginWithFacebookSDK(
            token,
            fieldsMapping: fieldsMapping,
            response: { (user: BackendlessUser!) -> Void in

                print("user: \(user)")
                self.backendless.userService.setStayLoggedIn(true)
                loadUser()
                // Access the storyboard and fetch an instance of the view controller
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let viewController: MainScreenViewController = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! MainScreenViewController

                let rootViewController = self.window!.rootViewController! as UIViewController
                print(rootViewController)
                rootViewController.presentViewController(viewController, animated: true, completion: nil)


            },
            error: { (fault: Fault!) -> Void in
                print("Server reported an error: \(fault)")
        })
    }

    return result
}

错误发生在rootViewController.presentViewController(viewController,animated:true,completion:nil)

我尝试在 viewDidAppear() 中的 viewController 中执行 segue,并且还尝试了 PerformSegueWithIdentifier() 但错误仍然存​​在。 如果我终止我的应用程序并重试,segue 工作正常。请建议我问题出在哪里。提前致谢。

最佳答案

我认为没有 rootViewController 所以你需要像下面这样设置

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

 var rootView: MyRootViewController = MyRootViewController()

 if let window = self.window{
        window.rootViewController = rootView
 }
 return true
}

然后在后记中使用它。

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value on performing segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38115389/

相关文章:

iOS 视频播放器擦洗图像

iphone - 我可以让 UIControl 取得 "disposable"事件目标的所有权吗?

ios - swift API认证

ios - 消除 UINavigationBar 和 UIVisualEffectView 之间独特的模糊边缘

ios - 滑动以使用 Segue 编辑详细信息

ios - Sonar 无法在Mac EI Caption上连接到mysql

ios - 具有 OpenSSL 通用依赖的 Swift 框架

swift - 如何使用按钮打开/关闭 ARKit?

ios - 删除或关闭由 segue 到达的 ViewController 的正确方法

ios - IOS开发中的Segue是什么?