1

I have one view controller with out navigation controller. Its name is LoginViewController. In my AppDelegate, I want to keep my LoginViewController as root view controller.

How can I do this in Objective-C? How can I set my view controller as root view controller?

Note: My view controller does not have navigation view controller. It's a single view controller.

Brian
  • 14,610
  • 7
  • 35
  • 43
mack
  • 603
  • 2
  • 9
  • 22

3 Answers3

2

do like

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. get the Storyboard Name
UIStoryboard* main = [UIStoryboard storyboardWithName:@"Main"
                                               bundle:[NSBundle mainBundle]];
//2. get the ViewController using Storyboard ID
UIViewController *viewConr = [main instantiateViewControllerWithIdentifier:@"HomeViewController"]; 
// 3.finally assign the Root
self.window.rootViewController = viewConr;
[self.window makeKeyAndVisible];
return YES;
}

for E.g

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

Without storyboard:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    LoginViewcontroller *Vc = [[LoginViewcontroller alloc]init];
    self.window.rootViewController = Vc;
    [self.window makeKeyAndVisible];
    return YES;
}

If using storyboard just make that view controller as initial view controller from storyboard.

enter image description here

Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
0

If you want to set home page VC as root vc from login page VC

Import appdelegate in login page

#import "AppDelegate.h"//Import in Login page VC
self.delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; // In viewDidLoad

Write below code where you navigate

//Make root view controller
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Home"];//Your Home page story board ID
self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.delegate.window makeKeyAndVisible];
Naresh
  • 16,698
  • 6
  • 112
  • 113