mack
mack

Reputation: 611

How to set the view controller as root view controller in AppDelegate

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.

Upvotes: 1

Views: 1208

Answers (3)

Naresh
Naresh

Reputation: 17902

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];

Upvotes: 0

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

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

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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

Upvotes: 2

Related Questions