0

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

3 Answers 3

1

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

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

1 Comment

How do I check to see if the view controller was loaded on the navigation controller? What I did was go into the interface builder and edit the "view controller (root view controller)" on my navigation controller and set its "nib" to my "FirstViewController". But it says that "navigationController is not declared" on that line.
0

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];

Comments

0

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"

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.