1

I'm creating an application that has 2 main view controllers at the moment. The app loads into the initial viewController, and clicking a button inside should bring up the second viewController. Here's what I have:

AppDelegate.h

#import <UIKit/UIKit.h>
#import "ViewController1.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController1 *mainViewCtr;
@property (strong, nonatomic) UINavigationController *navigationController;
@end

AppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    _mainViewCtr = [[ViewController1 alloc] initWithNibName:@"mainViewCtr" bundle:nil];
    _navigationController = [[UINavigationController alloc] initWithRootViewController:_mainViewCtr];
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _window.rootViewController = _navigationController;
    _navigationController.delegate = self;
    _navigationController.navigationBarHidden = YES;
    [_window addSubview:_navigationController.view];
    [self.window makeKeyAndVisible];
}

and my button method inside viewcontroller1:

- (IBAction)SessionNickNameSubmit:(id)sender {
    ViewController2 *secondViewCtrl = [[ViewController2 alloc] initWithNibName:@"secondViewCtrl" bundle:nil];

    [self.navigationController pushViewController:secondViewCtrl animated:YES];
}

but when I click the button the view doesn't change. I tried debugging and the code is hit, but nothing happens.

am I missing a setting somewhere?

UPDATE

I've updated all viewController variable names:

instead of ViewController1/2 I'm using mainViewCtrl and secondViewCtrl

but still no use :(

Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127

4 Answers4

5

You made a typo:

it's

_window.rootViewController = _navigationController;

not

_window.rootViewController = _joinViewController;

And NeverHopeless's suggestion is also spot on. It's probably the typo AND the fact that you add your second viewcontroller as ViewController2 and not using a proper variable name.

Another suggestion is making a storyboard (if you are not using one) and adding a segue for the transition. Simply assign the segue processing to the button. Like this:

-(IBAction)SessionNicknameSubmit:(id)sender
{
    [self performSegueWithIdentifier:@"identifier" sender:self ];
}

Here is a nice description of how it works and how to use it plus some useful pointers!

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
1

Obj-C is a case sensitive language, class name and instance name should not be the same like ViewController2. Try like this:

- (IBAction)SessionNickNameSubmit:(id)sender {
    ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

    [self.navigationController pushViewController:viewController2 animated:YES];
}
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0

The reason is that you have set the window's rootViewController to ViewController1.

You need to set you navigation controller to the window's rootViewController.

So that when you try to access the self.navigationController on the press of the button, it will access the navigation controller in which the self resides i.e. your window's rootViewController now.

Then it will push the next view controller properly.

Vikas Dadheech
  • 1,672
  • 12
  • 23
  • Do one thing... when you set the window's root view controller as the navigation controller, check the memory address for that object. And when you are trying to push the second view controller, at that time also check for the memory address for self.navigationController. Are both pointing to the same address? – Vikas Dadheech Aug 04 '15 at 08:14
0

After looking at almost every tutorial and every stack overflow answer, I finally found a solution that worked. I had to make an instance of the storyboard in the app delegate and use that to create my first view controller instance.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.joinViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:joinViewController];
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    navigationController.navigationBarHidden = YES;
    _window.rootViewController = navigationController;
    [_window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

I think the problem was that when I was creating an instance of ViewController, it was creating a new instance and binding the navigation controller to it (independent of the view controller that was showing up in the simulator). So when I was using the push method it wasn't recognizing self.NavigationController (that's why NSLog(self.NavigationController == nil) was logging 1

Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127