viewController 中的 pushViewController 和 tableViewController

2024-01-02

我有一个 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: method.

- (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属性返回零。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

viewController 中的 pushViewController 和 tableViewController 的相关文章

随机推荐