1

我有一个带有 3 个视图/选项卡的 TabBarController...在一个选项卡上我有一个 UITableView。现在我想如果用户点击一个单元格切换到一个详细视图......我已经用这个代码试过了:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailView"];

[self.navigationController pushViewController:nextController animated:YES];

}

但它确实有效……有什么想法吗?!

4

2 回答 2

1

您需要将表格视图控制器嵌入到导航控制器中

于 2012-09-19T12:40:44.570 回答
0

如果您想使用导航控制器,请这样做:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//for home tab.. u want to add navigation controller 
YourTableViewController *objviewController = [[[YourTableViewController alloc] initWithNibName:@"YourTableViewController_iPhone" bundle:nil] autorelease];
UINavigationController *navCtrl = [[UINavigationController alloc] initRootViewController:objviewController]; 

//for tab2...
YourSecondViewController *objYourSecondViewController =  [[[YourTableViewController alloc] initWithNibName:@"YourSecondViewController_iPhone" bundle:nil] autorelease];

//for  tab3...
YourThirdViewController *objYourThirdViewController =  [[[YourThirdViewController alloc] initWithNibName:@"YourThirdViewController_iPhone" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navCtrl,objYourSecondViewController,objYourThirdViewController,nil];

self.window.rootViewController=self.tabBarController;
  [self.window makeKeyAndVisible];
return YES;

}


但是如果不需要导航控制器,你也可以添加:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DetailViewController *nextController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailView"];

  [self.view addSubView:nextController];
}
于 2012-09-19T12:56:06.803 回答