您目前拥有的是 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];