Configuring RootViewController with UINavigationController Software

I have a program with a navigation controller and a default RootViewController. If I do nothing programmatically, the application starts up and the RootViewController works as I expect, for example, as shown below:

enter image description here

The problem I encountered is to enable an additional StartController. I want this: In my AppDelegate (didFinishLaunchingWithOptions), I want to have a code like this:

UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"OptionalStartViewController"]; self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; 

To show the optional start of the ViewController first. Then, after the user finishes working with the additional view controller, they will be able to display the RootViewController.

So, in the optional launch of ViewController, I added code similar to this to show the Root View controller:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"]; [self appDelegate].window.rootViewController = viewController; [[self appDelegate].window makeKeyAndVisible]; 

All this works except when the RootViewController, if shown, does not have navigation controls as expected (i.e. the view is displayed without navigation controls).

I also tried the code below (using UINavigationController instead of ViewController) with the same results ...

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; [self appDelegate].window.rootViewController = viewController; [[self appDelegate].window makeKeyAndVisible]; 

Another twist ... there may be a few additional initial view controllers.

Any ideas?

+6
source share
3 answers
  • Delete UINavigationController
  • select in our case the controller ("additional start controller")
  • click Editor >> Embed In >> Navigation Controller
  • Now select the Navigation Controller and the right side, select the Is Initial View Controller option

If you want to dynamically change the root view controller, it is better to do this programmatically, in didFinishLaunchingWithOptions , get an instance of the window, initialize the navigation controller with the root view controller, and then install the Windows root controller on the navigation controller. Here is the code.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; OptionalFirstViewController *optionalFirstViewController = [[OptionalFirstViewController alloc] initWithNibName:@"OptionalFirstViewController" bundle:nil]; UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:optionalFirstViewController]; [optionalFirstViewController release]; self.window.rootViewController = homeNavigationController; [homeNavigationController release]; [self.window makeKeyAndVisible]; return YES; } 

and make sure you uncheck Is Initial View Controller for all view controllers in the bulletin board

Hope this helps

+4
source

Without using storyboards, we can programmatically program in the AppDelegate class, just write these lines of code in AppDelegate.

Appdelegate.h

 @property (strong, nonatomic) UIStoryboard *storyboard; @property (strong, nonatomic) UINavigationController *navigationController; 

AppDelegate.m

 if(self.window == nil) self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; if(self.navigationController == nil) self.navigationController = [[UINavigationController alloc]init]; if(self.storyboard == nil) self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; self.navigationController.navigationBarHidden = YES; // Here we can check user is login or not also, UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; ViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; [self.navigationController pushViewController:ivc animated:YES]; self.window.rootViewController = ivc; [self.window setRootViewController:self.navigationController]; [self.window makeKeyAndVisible]; 

The quick version

 var window: UIWindow? var storyBoard :UIStoryboard? var navigationController : UINavigationController? var mainController :MainViewController = MainViewController() if window == nil { window = UIWindow(frame: UIScreen.main.bounds) } if navigationController == nil { navigationController = UINavigationController() } if storyBoard == nil { storyBoard = UIStoryboard(name: "Main", bundle:nil) } navigationController?.setNavigationBarHidden(true, animated: true) // storyboard with identifer mainController = storyBoard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController navigationController?.pushViewController(mainController , animated: true) window?.rootViewController = navigationController window?.makeKeyAndVisible() 
+1
source

You cannot install the new root view controller on the UINavigationController after it is initialized. You should modify the viewControllers array of the UINavigationController to suit your needs.

If you want the additional VC to be the root VC, create @[optionalVC] and set it as a viewControllers array of UINavigationController .

0
source

Source: https://habr.com/ru/post/982334/


All Articles