不明白如何在iPhone中使用导航控制器

时间:2011-09-08 13:10:59

标签: iphone navigationcontroller

我对iphone非常陌生,我有以下误解。

互联网上有关如何以编程方式使用NavigationController的教程说:

 NavigationController must be declared in applicationDidFinishLaunching and must be init with a root.After that you can add views to it.

我有这个:    UIViewController类意为(AdiViewController.h, AdiViewController.m and AdiViewController.xib)而不是Delegate file意味着没有applicationDidFinishLaunching方法。

我想做的是从我的班级 - AdiViewController按下按钮转到另一个视图。 我知道我需要一个NavigationController来保留我的观点,其中包含AdiViewController

但我的问题是我应该在NavigationController中初始化viewDidAppear的位置?...因为我没有Delegate个文件。

如果你可以用我的这个小问题提供一个最小的例子,那就太棒了。我很有信心,对于那些高级人员来说这是什么,但我仍然没有得到它。谢谢

1 个答案:

答案 0 :(得分:2)

NavigationController必须在applicationDidFinishLaunching中声明 - >这不是真的。 在你的AdiViewController中,如果按下按钮时你想要加载导航控制器吗?

// Hook this IBAction to your button in AdiViewController
- (IBAction)pushNavController
{
   AnotherViewController* rootView = [[AnotherViewController alloc] initWithNibName:@"Anotherview" bundle:nil];
   UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:rootView];
   [rootView release];
   [self presentModalViewController:navController animated:YES];
   [navController release];
}

如果你在AnotherViewController中,即你在导航控制器的根视图控制器中。您需要从那里推送和弹出视图控制器。例如,如果你在AnotherViewController中有一个按钮:

// push next view controller onto navigation controller's stack
    - (IBAction)pushNextViewController
    {
      NextViewController* nextView = [[NextViewController alloc] initWithNibName:@"NextView" bundle:nil];
      [self.navigationController pushViewController:nextView animated:YES];
      [nextView release];
    } 

// Similarly if you want to go back to AnotherViewController from NextViewController you just pop that from navigation controller's stack
- (IBAction)pushNextViewController
    {
      [self.navigationController popViewControllerAnimated:YES];
    }