从 "nested" 视图控制器启动视图控制器

Starting a View Controller from a "nested" View Controller

我有一个 UIViewController,它添加了另一个 UIViewController 作为子视图:

#import "ListViewController.h"

@interface SearchViewController ()

@end

@synthesize listController;

- (void)viewDidLoad {
    [super viewDidLoad];

    …

    self.listController =[[ListViewController alloc] init];
    [self.view insertSubview:listController.view belowSubview:mainTabBar];
    self.listController.view.frame = CGRectMake(0, tabHeight, screenWidth, (screenHeight-tabHeight));

}
@end

ListViewController 中有一个 TableView。单击 TableView 中的项目时,我想将 UIViewController 添加到导航控制器:

#import “PlaceViewController.h"

@interface ListViewController ()

@end

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlaceViewController *vc = [[PlaceViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

如果 ListViewController 正常添加到 Navigation Controller,则此方法有效。但是,在这种情况下,当 ListViewController 基本上 "nested" 在另一个中时,新添加的 PlaceViewController 没有打开。有没有办法使这项工作?谢谢

您应该在容器和添加了视图的控制器之间创建 parent/child 关系。一些信息可以在 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/ 的介绍中找到,更多信息可以在 Internet 上找到。只要看看自定义容器视图控制器。

然后您可以使用 ListViewController 的 parentViewController 属性 从您的 ListViewController 获取容器的 navigationController 属性。