Search code examples
iosswiftiphonestoryboardappdelegate

Change first viewController presented (main interface)


Hello i have done a page controller for my app. This page controller will appear at the app launch if there isn't any "User" core data object on the device.

So want i want to do is : When app launch, do a core data request that return all "User" objects in a array. (this part is ok) If the array is empty the VC presented is the pageController else that's the normal on whose presented.

The pageController and the other controller are on different storyboards. I have seen we can change the first storyboard in the app parameter, that's parameter is called main interface. Actually main interface is the normal VC, so how can i present the page controller programmaticly in my didFinishLaunchingWithOptions() when my condition is true ?


Solution

  • Inside didFinishLaunchingWithOptions

    // query coredata 
    
    if arr.isEmpty { 
      let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pageID") as! PageController
      self.window?.rootViewController = viewController
    }
    else {
      let otherController = UIStoryboard(name: "Other", bundle: nil).instantiateViewController(withIdentifier: "otherID") as! OtherController 
      self.window?.rootViewController = otherController 
    }