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 :(

duxfox--
  • 9,097
  • 15
  • 55
  • 116

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!

Joze
  • 3,250
  • 3
  • 35
  • 52
  • thanks for the help, but its not working, I've updated my variable names (see my updated question) – duxfox-- Aug 03 '15 at 12:31
  • Update the code in your question with the new corrections made plz. We can have a better picture then of what is wrong. – Joze Aug 03 '15 at 12:32
  • @AbdulAhmad you write this statement _window.rootViewController = _navigationController;. you understand or not? – Dharmesh Dhorajiya Aug 03 '15 at 12:34
  • @DharmeshDhorajiya yea I think so. – duxfox-- Aug 03 '15 at 12:36
  • _navigationController.delegate = self; remove this line in your code @AbdulAhmad – Dharmesh Dhorajiya Aug 03 '15 at 12:38
  • no my bad, they're not in my code, just on here, I'll fix them – duxfox-- Aug 03 '15 at 12:39
  • @DharmeshDhorajiya ok I removed it but still no luck – duxfox-- Aug 03 '15 at 12:40
  • by the way I added an `NSLog(@"%i", self.navigationController == nil);` in my button click method and its logging `1` so does that mean it can't recognize navigationController? – duxfox-- Aug 03 '15 at 12:41
  • @AbdulAhmad I made an edit to my answer with another suggestion. – Joze Aug 03 '15 at 12:42
  • I just did this and it works. But I'd still like to figure out how to do it programmatically with 2 separate .XIB files – duxfox-- Aug 03 '15 at 12:43
  • @AbdulAhmad Yes of course. That's the purpose of the storyboard. To make the story of your application. You specify there what are the viewcontrollers of your navigationcontroller and how will they transition from one another. – Joze Aug 03 '15 at 12:44
  • @AbdulAhmad I added a link for you in the answer to check how storyboards function – Joze Aug 03 '15 at 12:48
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
  • 10,869
  • 4
  • 34
  • 55
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,604
  • 10
  • 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

duxfox--
  • 9,097
  • 15
  • 55
  • 116