2

我有一个 UIViewController,这个控制器包含在一个 navigationController 中。我在这个 viewController 中添加了一个 UITableViewController。当我按下 tableView 的单元格时,我想调用 pushViewController 方法。

我试过这个:

UITableViewController


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
  FirstView *myViewController = [[FirstView alloc] init];
  [f myViewController];
}

UIViewController(第一视图)


-(void)pushIt
{
    SecondView *sCont = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sCont animated:YES];
    NSLog(@"didSelect"); // is printed
    [sCont release];
    sCont = nil;
}

但什么也没有发生。我把 NSLog() 放到我的 pushIt 方法中,我可以看到它。所以我不明白为什么我不能推动它。

任何的想法?

4

2 回答 2

1

UIViewController有一个名为的属性,如果它调用的视图控制器存在navigationController,它将返回一个。UINavigationController

使用此属性,您可以将视图控制器从表视图的didSelectRowAtIndexPath:方法推送到导航堆栈。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    SecondView *sCont = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sCont animated:YES];
    [sCont release];
}

您当前的代码无法正常工作的原因可能是以下原因:

  • 您已经有一个 FirstViewController 的实例,正如您所说,您已经添加了一个表视图作为其子视图
  • 当用户点击不在导航堆栈上的单元格时,您尝试创建 FirstViewController 的新实例,因此尝试从那里将视图控制器推送到堆栈上是行不通的,因为该navigationController属性返回 nil。
于 2010-02-10T09:31:09.863 回答
0

您分配并初始化 myViewController,但从不将其推送到导航或窗口或其他任何内容,然后将 sCont 推送到 myViewController,这在窗口中不存在。首先,尝试不使用 myViewController,然后尝试将 myViewController 推送到导航,然后再将 sCont 推送到其中。

于 2010-02-10T09:31:20.487 回答