viewController 中的 pushViewController 和 tableViewController
题
我有一个 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 方法中,我可以看到它。所以我不明白为什么我不能推动它。
任何想法?
解决方案
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
属性返回零。
其他提示
您的alloc和init myViewController但从来没有把它推到导航或窗口或任何其他,那么你推到SCONT myViewController,这是不存在的窗口。首先,尝试不使用myViewController,接下来尝试推动SCONT到它之前推myViewController到导航。
不隶属于 StackOverflow