在viewController中使用带有tableViewCon

在viewController中使用带有tableViewCon

本文介绍了在viewController中使用带有tableViewController的pushViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an UIViewController, this controller is contained in a navigationController.I add an UITableViewController in this viewController. I would like to call a pushViewController method when I press on a cell of my tableView.

我尝试过这个:

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

But nothing happen. I put NSLog() to my pushIt method and I can see it. So I don't understand why I can't push it.

有什么主意吗?

推荐答案

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

UIViewController has a property named navigationController that will return a UINavigationController if one exists for the view controller its called from.

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

Using this property, you can push view controllers onto the navigation stack from your table view's 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];
}

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

The reason your current code isn't working is probably because of the following:

  • 您已经有了FirstViewController的实例,就像您说的那样,已将表视图添加为其子视图
  • 您尝试在用户点击导航堆栈上不存在的单元时创建FirstViewController的新实例,因此尝试将视图控制器从那里推到堆栈上是行不通的,因为属性返回nil.

这篇关于在viewController中使用带有tableViewController的pushViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:24