参考以下代码。如果你想使用 pushViewController 方法。你必须有一个 NavigationViewController。所以,你的结构有点复杂。一个 ViewController 有两个 TableViewController。一个 ViewController 没有 NavigationController。NavigaitonViewController 在运行时必然属于应用程序,因为它应该被配置。
TwoTableViewsAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *naviController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
[window setRootViewController:naviController];
[window makeKeyAndVisible];
return YES;
}
TwoTableViewsViewController.m
- (void)viewDidLoad {
if (firstController == nil) {
firstController = [[FirstTVContoller alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTVController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];
[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
firstController.rootViewController = self;
secondController.rootViewController = self;
[super viewDidLoad];
}
FirstTVContoller.h , SecondTVController.h
#import <Foundation/Foundation.h>
@interface FirstTVContoller : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *items;
}
@property (nonatomic, retain) UIViewController *rootViewController;
@end
FirstTVContoller.m , SecondTVController.m
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VerticalDetailViewController *verticalDetailViewController = [[VerticalDetailViewController alloc] initWithNibName:@"VerticalDetailViewController" bundle:nil];
[[self.rootViewController navigationController] pushViewController:verticalDetailViewController animated:YES];
}