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

6
  • @Sujania I commented it out and its still not working Commented Aug 3, 2015 at 12:18
  • 1
    What is your _joinViewController? And yes you need _navigationController.delegate = self. Commented Aug 3, 2015 at 12:20
  • i think your mistake is here _window.rootViewController = _joinViewController;// chage this statement to _window.rootViewController = _navigationController. @Abdul Ahmad Commented Aug 3, 2015 at 12:21
  • _window.rootViewController = _navigationController; Commented Aug 3, 2015 at 12:21
  • I see, I'll try and get back to you guys Commented Aug 3, 2015 at 12:22

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

12 Comments

thanks for the help, but its not working, I've updated my variable names (see my updated question)
Update the code in your question with the new corrections made plz. We can have a better picture then of what is wrong.
@AbdulAhmad you write this statement _window.rootViewController = _navigationController;. you understand or not?
@DharmeshDhorajiya yea I think so.
_navigationController.delegate = self; remove this line in your code @AbdulAhmad
|
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];
}

Comments

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.

1 Comment

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?
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

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.