2

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

我尝试这样做:

的UITableViewController


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

的UIViewController(的firstView)


-(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方法中,我可以看到它。所以我不明白为什么我不能推它。

任何想法?

+0

反正什么? – willcodejavaforfood 2010-02-10 09:33:55

+0

Oups我将f更改为myViewController var name以获得更多理解,但我忘记在我的调用中更改它。所以:[myViewController pushIt] – Pierre 2010-02-10 11:55:30

回答

1

UIViewController有一个名为navigationController的属性,如果存在一个名为from的视图控制器,将返回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属性返回零。
+0

感谢您的答案。 Jasarien,我把你的代码,但没有任何反应。这很奇怪... – Pierre 2010-02-10 09:39:19

+0

这是因为你的表视图控制器不是导航堆栈的一部分。它的视图仅作为FirstViewController视图的子视图添加。为什么表视图控制器不是根视图控制器的原因是什么?您的FirstViewController是否包含除表格视图以外的UI? – Jasarien 2010-02-10 09:48:36

+0

哼,不,但我想自定义我的视图只是为了修复元素,例如:table.view.frame = CGRectMake(20,20,300,300); 并自定义它周围的所有元素(例如图像)。 – Pierre 2010-02-10 11:11:27

0

你的alloc和init myViewController但从来没有把它推到导航或窗口或任何其他,那么你推到SCONT myViewController,这是不存在的窗口。首先,不要尝试使用myViewController,接下来尝试将myViewController推送到导航,然后再将sCont推入它。