2

我有一个UITableViewController内一个UIViewController。虽然这个 table viewcontroller 是唯一涉及的,但当用户点击一行时,它可以很好地推送视图。然而,自从我把它移到 中包含的两个之一之后UIViewController,行的水龙头突然什么都不做。

试过四处寻找,但我不是第一个遇到这个问题的人,但没有一个答案适合我的情况,或者这些问题没有有效的答案。该链接是我找到的最接近的链接,但我没有使用故事板——我使用的是单独的 XIB。

那么如何从视图控制器中的视图控制器推送新视图?

回顾一下:

  1. 这就是我所拥有的,它可以很好地将用户带到一个新屏幕!

    // Normal table behavior, as illustrated by [another question][2].
    
    - (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        SomeView *detailViewController = [[SomeView alloc] initWithNibName:@"SomeView" bundle:nil];
    
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
    }
    
  2. 现在我将视图控制器作为视图中的一个属性——上面的代码在 tableviewcontroller 的文件中而不是在“主”视图中,不会导致新屏幕出现了!


感谢您的评论!这是一些代码来阐明我的情况。

  1. 控制器中的控制器。这是我用来测试概念的测试项目中的一个文件。在这种情况下,我在 tableview 控制器中有一个 tableview 控制器。

    @interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    
    // This is the controller within the controller
    @property IBOutlet SecondTableViewController *secondTableController;
    @property IBOutlet UITableView *secondTable;
    
  2. SecondTableViewController有这个有趣的一点。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.
    
        UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"SimpleNonTableViewController" bundle:nil];
        // ...
        // Pass the selected object to the new view controller.
        [manualViewControllerParent.navigationController  pushViewController:detailViewController animated:YES];
    }
    

用户与之交互的视图与SimpleTableViewController. 这样,SecondTableViewController就是“内”了SimpleTableViewController。如果您想了解更多详细信息,请随时发表评论!


我已将我的测试/概念项目放在 github 上。 https://github.com/hyliandanny/TableViewCeption

4

2 回答 2

3

您需要使用自定义容器控制器来执行您想要的操作。如果您使用情节提要,这将是最简单的,但您也可以使用 xibs 在代码中完成。外部控制器应该是 UIViewController,而不是表视图控制器。您可以执行以下操作(在外部控制器中):

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"SimpleNonTableViewController" bundle:nil];
    [self addChildViewController:detailViewController];
    detailViewController.view.frame = set the frame to what you want;
    [self.view addSubview:detailViewController.view];
    [detailViewController didMoveToParentViewController:self];
}

您应该阅读 Apple 的自定义容器控制器文档。

于 2013-01-23T01:23:39.553 回答
0

您需要确保:

  • 您的UITableView委托已连接到您的控制器。否则它不会调用didSelectRow. 您可以在 xib 或 viewDidLoad 方法中执行此操作。
  • 你的 self.navigationController 不是 nil
  • 你的 detailViewController 不是 nil

我也认为你的意思是你UITableViewUIViewController. UITableView只是视图,而UITableViewController控制器。您不能在另一个控制器中拥有一个控制器。

于 2013-01-23T00:18:16.110 回答