How to set Root View Controller programatically in Objective C?
Asked Answered
L

4

6

I am a newbie in iOS Development trying to learn how to create and set views programmatically.

i am trying to do swift statement in Obj-C

window?.rootViewController = UINavigationController(rootViewController : ViewController()) 

Project: Single View Application . Trying to link default Created ViewController.h

As per Krunals Answer i updated code but Navigation Controller is not shown in simulator

Cmd+Click on controller does not navigate to ViewController File

#import "AppDelegate.h"
#import "ViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIScreen *screen=[[UIScreen alloc]init];
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.makeKeyAndVisible;




    ViewController *controller = [[ViewController alloc] init];



    window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;
Liquidation answered 28/8, 2017 at 17:24 Comment(2)
You're referring to two different windows in your function. The local window that you are creating and self.window which is a property on your app delegate which is probably nil.Draftee
@Draftee i changed *window to *window2 self.window2.makeKeyAndVisible window2.rootViewController=... But still navigation controller not shownLiquidation
G
1

Initialise your view controller ViewController before you add (use as root controller of navigation) into navigation controller stack.

Here is sample code to initialise simple view controller

UIViewController *controller = [[UIViewController alloc] init];

Here is sample code to initialise using storyboard

ViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"<ViewController - string identifier of your view controller>"];

Here is sample code to initialise using NIB/Bundle

ViewController *controller = [[ViewController alloc] initWithNibName:@"<ViewController - string NIB name>>" bundle:nil];

According to your code and following comment try this code only (remove other codes from your app delegate launch):

// make sure your NIB name is 'ViewController' 

ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
if (controller != nil) {
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: controller];
    self.window.makeKeyAndVisible;
} else {
   //print - your view controller is nil
}
Greig answered 28/8, 2017 at 17:25 Comment(7)
Thanks for reply iam a newbie ,forgive me . if i add UIViewController *ViewController = [[UIViewController alloc] init]; in code does that mean *ViewController holds ViewController.m automatically generated by Single View Project?Liquidation
Iam trying to do without storyboardLiquidation
No, that (first one) won't work for your code. You need to identify which one will work for your. Using storyboard or NIB (Bundle)?Greig
So, you should initialise your view controller using NIB/Bundle. Also make sure your NIB name is 'ViewController'Greig
Sorry Iam not familiar with NIB/Bundle . iam trying to do swift statement in Obj-C window?.rootViewController = UINavigationController(rootViewController : ViewController()) .Clicking on ViewController() leads to ViewController.swift , iam trying same functionality in obj cLiquidation
If your view controller has the same name as its nib then you don't need to specify the nib name, you can just do [[ViewController alloc] init]Draftee
@Draftee i tried it but Ctrl+click does not navigate to ViewController.m . Is it normal? Navigation Controller is not shown on simulatorLiquidation
L
1

Thanks to Krunal for detailed answer .

Thanks to dan for support

i found issue instead of self.window.rootViewController , i typed window.rootViewController.

setting self.window.rootViewController solved issue.

i dont know difference between self.window.rootViewController and window.rootViewController and reason for issue.

If some one knows answer please provide answer on comment

#import "AppDelegate.h"
#import "ViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIScreen *screen=[[UIScreen alloc]init];
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    self.window.makeKeyAndVisible;




    ViewController *controller = [[ViewController alloc] init];





    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;
Liquidation answered 28/8, 2017 at 18:33 Comment(1)
I think self refers to your UIApplication objectFrontier
C
0

Delele the file Main.storyboard and let the Main interface option empty before you do it. enter image description here

And add this code:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

UPDATE: If you want to use storyboard with UINavigationController, try this:

enter image description here

Copyist answered 29/8, 2017 at 9:30 Comment(2)
What do you mean delete the Main storyboard file? How is that relevant to the issue?Megalomania
@n00bProgrammer I have updated my answer, please check it.Copyist
T
0

Adding UIViewController and adding with UINavigationController

      UIStoryboard *storyboard = self.window.rootViewController.storyboard;
           UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];
            [self setRootViewController:rootViewController];


        #pragma mark - Set RootView Controller
        -(void)setRootViewController:(UIViewController *)rootViewController {
            self.window.rootViewController = rootViewController;
            [self.window makeKeyAndVisible];
        }


         UIStoryboard *storyboard = self.window.rootViewController.storyboard;
           UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    [self setRootViewController:navController];

-(void)setRootViewController:(UINavigationController *)rootViewController {
                self.window.rootViewController = rootViewController;
                [self.window makeKeyAndVisible];
            }
Turboelectric answered 29/8, 2017 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.