【问题标题】:PresentViewController is showing nil of my RootViewControllerPresentViewController 显示我的 RootViewController 为零
【发布时间】:2016-12-07 22:11:07
【问题描述】:

所以让我解释一下。 我有两个 ViewController(登录页面和主页)。 登录页面包含两个按钮(FB 和 G+ 登录按钮)。 使用登录凭据一切正常,并呈现下一个视图,即主页。 在主页面中,我有按钮(退出)。按钮应该像唱歌一样让我回到登录页面,不幸的是它不起作用。 下面是我的代码

let vc = self.storyboard?.instantiateViewController(withIdentifier: "LoginPage") as! LoginPage
            vc.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
            self.present(vc, animated: true, completion: nil)

正在显示

fatal error: unexpectedly found nil while unwrapping an Optional value

主页面中所有声明的 IBOutlet 的输出为 nil

请问我需要帮助吗!!!??

【问题讨论】:

  • 再次检查登录视图控制器的情节提要 ID。它必须完全匹配“LoginPage”,大小写,没有空格。此外,它必须在同一个情节提要中。
  • 没有错字问题...另一件事是,当我更改代码时: let LoginControllerx = LoginPage() UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: LoginControllerx) 它显示 IBOutlet nil登录页面:(
  • @EdisShabixhiza 一个可能的问题是您有一些未连接的插座。
  • @i6x86 我已经仔细检查并且我的插座已连接,但感谢您的快速响应

标签: ios swift xcode


【解决方案1】:

首先我们验证视图控制器是否在Identity inspector 上有一个Storyboard ID

然后:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = storyboard.instantiateViewController(withIdentifier: "LoginPage")

let window = UIApplication.shared.windows[0] as UIWindow

UIView.transition(with: window,
                  duration: 0.5,
                  options: UIViewAnimationOptions.transitionCrossDissolve,
                  animations: {
                       window.rootViewController = loginViewController
        }, completion: nil)

这样,您将替换为您的登录视图控制器呈现的视图控制器。

【讨论】:

  • @pablerios 上帝送你:)
  • 嗨@EdisShabixhiza,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
【解决方案2】:

当您注销应用程序时,尝试使用通知来更改根目录。

首先在你的 appDelegate addObserver 中:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Notification used in logout action
    let notf = NSNotificationCenter.defaultCenter()
    notf.addObserverForName("logout", object: nil, queue: NSOperationQueue.mainQueue()) { (notification) in
       let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginPage") as! LoginPage
       self.window?.rootViewController = controller
    }
    return true
}

然后在 logoutAction 中简单地 postNotification

func logout() {
    NSNotificationCenter.defaultCenter().postNotificationName("logout", object: nil)
} 

【讨论】:

  • 我也会试试这个选项:),让你知道结果
猜你喜欢
  • 1970-01-01
  • 2012-11-05
  • 2017-06-18
  • 2014-11-20
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
相关资源
最近更新 更多