3

I am trying to change the root view controller from the app delegate's didFinishLaunchingWithOptions, depending on whether the user is logged in or not. Once I get past this condition, I am using the following code to change root view controllers:

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController

self.window?.makeKeyAndVisible()

However, when I launch the app (with a valid logged in user) the simulator first shows the log in screen (old root view controller) for a second, then the screen goes black for about 30 seconds to a minute, before finally showing the desired view controller.

The view controller structure in storyboard is as follows:

SWRevealViewController -> Navigation Controller -> Desired View Controller (new root)

The reason for beginning with SWRevealViewController is because the slide menu is lost otherwise.

Any ideas of what might be going on?

rodrigochousal
  • 401
  • 4
  • 19

4 Answers4

2

I found a way to produce a similar result to the one I desired. It involves not changing the root view controller at all, and having it present an "artificial root view controller" after launch:

if let currentRoot = self.window?.rootViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let artificialRoot = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
    currentRoot.present(artificialRoot, animated: false, completion: nil)
}

Although not ideal, the results of this implementation are by far better than the implementation in the question description.

rodrigochousal
  • 401
  • 4
  • 19
0

Here is an example, that works for me, just make your loginViewControllers to be a stack of UINavigationController, not related to SWRevealController. It is easy workaround.

self.window = UIWindow(frame: UIScreen.main.bounds)
if User.shared.firstLaunch {
     let navigationVC = Storyboard.inital.instantiateViewController(withIdentifier: "firstLaunchNC") as! UINavigationController
     self.window?.rootViewController = navigationVC
} else if User.shared.token == "" {
     let navigationVC = Storyboard.inital.instantiateViewController(withIdentifier: "initialVC") as! UINavigationController
     self.window?.rootViewController = navigationVC
} else {
     self.registerForPushNotifications(application: UIApplication.shared)
     User.shared.refreshToken()
     let revealController = Storyboard.main.instantiateViewController(withIdentifier: "revealViewController") as! SWRevealViewController
     self.window?.rootViewController = revealController
}

self.window?.makeKeyAndVisible()
return true
JuicyFruit
  • 2,638
  • 2
  • 18
  • 35
0

try this:

let viewController = ViewController()
let navigationController = UINavigationController(rootViewController: viewController)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
Niib Fouda
  • 1,383
  • 1
  • 16
  • 21
0

try this:

 UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
            tbc.selectedIndex=[singleton getSelectedTabIndex];
            // tbc.selectedIndex=0;
            //self.window.rootViewController = tbc;
            [self.window setRootViewController:tbc];

            [UIView transitionWithView:self.window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil

completion:nil];

If This not work..Please check your initial Controller ...In Which Never try Load NSAtrributedString

NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];

Note:-- Black Screen Apear When Initial Controller ViewDidLoad Take Some time to load UIview into Memory due some Lazy UI loading Assests are done in ViewDidLoad...

Try heavy load view cide into ViewWillApear or ViewDidApear ....