UINavigationController: A Simple Example

I am trying to make a very simple example of a UINavigationController. Here is my code:

- (void)viewDidLoad {
  [super viewDidLoad];

This next line works or at least doesn't explode.

  navController = [[UINavigationController alloc] initWithRootViewController:self];
  self.title = @"blah";

  PageOneController *one = [[[PageOneController alloc]init] autorelease];

Example 1. THIS LINE IS NOTHING

  [navController pushViewController:one animated:NO];

Example 2. THIS WORKS LINE (but, of course, there is no navigation controller)

  [self.view addSubview:one.view];
}

Why can't I push instances of ViewController on navController and see a screen change?

Note: I understand that I can have my own concepts back, and I don’t need my view to refer to UINavigationController... or something like that.

+5
source share
2 answers
- (void)viewDidLoad {
    [super viewDidLoad];

    PageOneController *one = [[[PageOneController alloc]init] autorelease];
    one.title = @"blah";
    navController = [[UINavigationController alloc] initWithRootViewController:one];
    [self.view addSubview:navController.view];
}

, , . , . , .

+11

@E-ploko , 100% ( ).

UINavigationController ( ). UINavigationController, rootViewController - (, "" ).

: , , .

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *one = [[UIViewController alloc] init];

    [one.view setBackgroundColor:[UIColor yellowColor]];
    [one setTitle:@"One"];

    navController = [[UINavigationController alloc] initWithRootViewController:one];
    // here  the key to the whole thing: we're adding the navController view to the 
    // self.view, NOT the one.view! So one would be the home page of the app (or something)
    [self.view addSubview:navController.view];

    // one gets reassigned. Not my clearest example ;)
    one = [[UIViewController alloc] init];

    [one.view setBackgroundColor:[UIColor blueColor]];
    [one setTitle:@"Two"];

    // subsequent views get pushed, pulled, prodded, etc.
    [navController pushViewController:one animated:YES];
}
+4

All Articles