介绍导航控制器

时间:2011-06-15 21:40:56

标签: iphone objective-c uinavigationcontroller

我正在尝试从常规视图控制器呈现NavigationController。当我呈现导航控制器时,我看到的只是默认导航栏(无论我在IB中设置它是什么),这让我觉得导航控制器在IB中不受欢迎。这是我打开导航控制器的代码:

NavigationController *navController = [[NavigationController alloc] initWithNibName:@"NavigationController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:navController animated:YES];

这是我的NavigationController标题:

@interface NavigationController : UINavigationController <UINavigationControllerDelegate> {
    UINavigationController *navigationController;
}

@property (nonatomic,retain) IBOutlet UINavigationController *navigationController;

@end

导航控制器在实现中合成。

在IB中,我有一个导航控制器,它连接到navigationController和文件的所有者委托。我究竟做错了什么?感谢

编辑:这就是它在IB中的样子: IB 这就是模拟器中的样子: Simulator

3 个答案:

答案 0 :(得分:2)

您目前拥有的是UINavigationController的子类。如果你想为UINavigationController添加自定义功能(比如在视图之间做不同的动画),那就是你想要做的。因为听起来你想要制作一个基于导航的应用程序,所以你实际上只想使用普通的UINavigationController类。以下是您需要做的事情,以便使用您想要的内容设置UINavigationController。 (我在Code中这样做,因为我讨厌在IB中设置UINavigationController)。

在你的applicationDidFinishLaunching中,你想要添加这段代码。

// (SomethingApplicationDelegate.m)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Assume that 'window' is your main window, and 'viewController' is the property
  // that is automatically created when you do a new view controller application.
  //
  // Also, assume that 'ContentViewController' is the name of the class that display
  // the table view. This will most likely be a subclass of UITableViewController
  ContentViewController *content = [[ContentViewController alloc] init];
  UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:content];
  [viewController.view addSubview:nvc.view];
  // This is the boilerplate code that is generated when you make a new project
  self.window.rootViewController = self.viewController;
  [self.window makeKeyAndVisible];
  return YES;
}

这将创建一个UINavigationController并将您的表视图设置为其内容。如果你想为新视图做好动画,你可以这样做

// This will be inside your ContentViewController
// Assume that 'NewViewController' is the class of the view controller you want
// to display
NewViewController *viewController = [[NewViewController alloc] init];
[self.navigationController pushViewController:viewController];
[viewController release]

这将自动将动画滑动到下一个视图控制器,同时制作漂亮的后退按钮,将您带回到ContentViewController

编辑:要使导航控制器显示为模态视图控制器,这就是您要做的。不要在应用程序委托中使用上面的代码,而是执行此操作。

-(IBAction)buttonPushed:(id)sender {
  // Assume this method is in a UIViewController subclass.
  //
  // The next two lines are copied from above in the Application Delegate, the same assumptions
  // apply about ContentViewController
  ContentViewController *content = [[ContentViewController alloc] init];
  UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:content];
  // This is the magic code
  [self presentModalViewController:nvc];
}

然后,当您完成导航控制器并希望它消失后,您可以这样做:

// Assume we are in some method in ContentViewController, or a similar view controller that is showing content
[self.navigationController dismissModalViewController];

答案 1 :(得分:1)

在viewDidLoad中声明导航控制器? 这样:

UINavigationController *navController = [[UINavigationController alloc]
                                           initWithRootViewController:view];

答案 2 :(得分:0)

默认情况下,

navigationController.view为空,您必须在那里放置一些东西。