【问题标题】:Deciding the initial view controller before app finish loading在应用完成加载之前决定初始视图控制器
【发布时间】:2015-10-30 11:47:56
【问题描述】:

您好,我有一个登录视图控制器和我的应用故事板视图。初始视图控制器指向应用仪表板。如果用户已登录,我将检查我的 appDelegate 应用程序功能。之后,我(重新)将正确的 UIViewController 设置为初始值。不知何故,应用程序总是转到初始视图控制器,然后说哎呀我需要登录,因为用户没有登录......我怎样才能避免这种情况?

这是我当前的代码。

dispatch_async(dispatch_get_main_queue(), {
    let storyboard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
    if (settings.isLoggedIn())
    {
        let vc: AnyObject! = storyboard.instantiateInitialViewController()
        self.window?.rootViewController = vc as? UIViewController
    } else {
        let vc: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("login")
        self.window?.rootViewController = vc as? UIViewController
    }
})

【问题讨论】:

  • 为什么是 dispatch_async ?
  • @Leonardo 这实际上是一个非常好的,我认为 xcode 会抱怨在接口上做一些事情而不确保它在主线程上。我把它拿掉了,现在流动真的很流畅。我认为那是我的错误。
  • 在辅助线程上做 UI 是有问题的,但是 appDelegate 方法总是在主线程上调用,所以你不必担心那里

标签: ios swift swift2


【解决方案1】:

如何在appdelegate下面的方法中检查用户是否登录

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

  //check user logged or not, if true you can set the initial view controller given below 

  let storyboard = UIStoryboard(name: "Main", bundle: nil)
  let vc : UIViewController! = storyboard.instantiateViewControllerWithIdentifier("urcontroller") 

  let window = UIApplication.sharedApplication().windows[0] ;
  window.rootViewController = vc;

  return true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多