I have searched far and wide for an answer to this question and would be really grateful of some help. I have removed the rest of my application to keep the question simple.
My root view is simply a blank view with one button. It has been initialised with a UINaviagationController. All I want to do is to have the button push a new view onto the stack when pressed. At the moment, the button does nothing when pushed, here is my code.
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Then in my first view controller:
#import "ViewController.h"
@implementation ViewController
-(IBAction)switchView:(id)sender
{
View2 *v2 = [[View2 alloc] initWithNibName:@"View2" bundle:nil];
[self.navigationController pushViewController:v2 animated:YES];
}
The -(IBAction) is configured properly, but still nothing, any ideas?
EDIT::
I have found a workaround to this.
It seems that when trying to access the NavigationController from the root view, using [self.navigationController…. ] will not send the message to the instance of navigationController (no idea why!).
I got it working by adding an ivar to ViewController of type UINavigationController, and then setting the ivar from AppDelegate after initializing ViewController, like such:
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
//The UINavigationController declared int AppDelegate.h
navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navController.view];
//Set the ivar on ViewController (navCon) to the UINavigationController we just
//initialized
self.viewController.navCon = navController;
Now when sending the message to the NavigationController in ViewController.m, I use the lines
View2 *v2 = [[View2 alloc] initWithNibName:@"View2" bundle:nil];
[navCon pushViewController:v2 animated:YES];
This does work, but I feel as though this is not the way it should be done, any thoughts?
Your code in
application: didFinishLaunchingWithOptions:is pretty messed up. You must make the navigation controller your window’srootViewController, not the nav controller’s root view controller. Then, the line[self.window addSubview:navigationController.view];is superfluous and should be removed.