Reputation: 9
I am following the tutorial here to use a UINavigationController to switch between ViewControllers. If I download the code and follow the tutorial it works all fine and dandy, but when I try to apply this to different apps I encounter the following confusion/issue. When I am in the "FirstViewController" how do I access [self navigationController]
if I do not have any navigationController in my viewController class? In this example, I cannot find out how or where the navigationController comes from in the FirstViewController. Can someone please explain how this works?
**Note: The full source code for the entire project can be found on the page I linked to above as well.
This is the line that confuses me:
[[self navigationController] pushViewController:secondViewController animated:YES];
This is the entire code for the viewController:
.h
#import <UIKit/UIKit.h>
@class SecondViewController;
@interface FirstViewController : UIViewController {
IBOutlet SecondViewController *secondViewController;
}
@property(nonatomic, retain) SecondViewController *secondViewController;
- (IBAction)PressMe:(id)sender;
@end
.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
@synthesize secondViewController;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"First View";
}
- (IBAction)PressMe:(id)sender
{
[[self navigationController] pushViewController:secondViewController animated:YES]; //This line
}
- (void)dealloc {
[super dealloc];
}
@end
Upvotes: 0
Views: 70
Reputation: 1021
If you are using IB (as you write in the code) right click the button, drag it to the SecondViewController and then select "Touch Up Inside"
Upvotes: 0
Reputation: 220
simply , you don't have any navigation controller, so you can't switch from view to another one with this
[[self navigationController] pushViewController:secondViewController animated:YES];
create navigation controller object in your FirstView and use it to move between views and in your PressMe method :
SecondViewController *secondView = [[secondView alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondView animated:YES];
[secondView release];
Upvotes: 0
Reputation: 1352
From Apple's Documentation:
If the receiver or one of its ancestors is a child of a navigation controller, this property contains the owning navigation controller. This property is nil if the view controller is not embedded inside a navigation controller.
So you don't have to declare the navigation controller. If the current view controller was loaded onto a navigation controller then the line you are referencing will provide you with the current navigation controller
Upvotes: 1