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] ;
ios
  • 165
  • 1
  • 11
  • 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. – dan Aug 28 '17 at 18:05
  • @dan i changed *window to *window2 self.window2.makeKeyAndVisible window2.rootViewController=... But still navigation controller not shown – ios Aug 28 '17 at 18:09

4 Answers4

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
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 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? – ios Aug 28 '17 at 17:30
  • Iam trying to do without storyboard – ios Aug 28 '17 at 17:30
  • 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)? – Krunal Aug 28 '17 at 17:32
  • So, you should initialise your view controller using NIB/Bundle. Also make sure your NIB name is 'ViewController' – Krunal Aug 28 '17 at 17:33
  • 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 – ios Aug 28 '17 at 17:38
  • 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]` – dan Aug 28 '17 at 17:48
  • @dan i tried it but Ctrl+click does not navigate to ViewController.m . Is it normal? Navigation Controller is not shown on simulator – ios Aug 28 '17 at 17:52
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] ;
ios
  • 165
  • 1
  • 11
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

liftlift
  • 59
  • 4
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];
            }