致命错误:在执行segue

时间:2016-06-30 06:24:49

标签: ios swift segue facebook-sdk-4.0

我正在使用facebook SDK登录,所有内容都已正确设置。当我第一次授权我的应用程序时,segue会执行到我的下一个视图控制器。但是当我注销并尝试再次登录时,segue会出错

致命错误:在解包可选值时意外发现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工作正常。请告诉我问题出在哪里。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

 var rootView: MyRootViewController = MyRootViewController()

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

然后使用它。