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] ;
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. Commented Aug 28, 2017 at 18:05
  • @dan i changed *window to *window2 self.window2.makeKeyAndVisible window2.rootViewController=... But still navigation controller not shown Commented Aug 28, 2017 at 18:09

4 Answers 4

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

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?
Iam trying to do without storyboard
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)?
So, you should initialise your view controller using NIB/Bundle. Also make sure your NIB name is 'ViewController'
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 c
|
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] ;

1 Comment

I think self refers to your UIApplication object
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

2 Comments

What do you mean delete the Main storyboard file? How is that relevant to the issue?
@n00bProgrammer I have updated my answer, please check it.
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];
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.