【问题标题】:Starting a View Controller from a "nested" View Controller从“嵌套”视图控制器启动视图控制器
【发布时间】:2015-07-05 19:28:24
【问题描述】:

我有一个 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 基本上“嵌套”在另一个中时,新添加的 PlaceViewController 不会打开。有没有办法使这项工作?谢谢

【问题讨论】:

  • 由于你的主控制器就像一个容器,你必须在容器和添加了视图的控制器之间创建父/子关系。您应该在 Internet 上查看一些关于自定义容器视图控制器的教程/文档。
  • 你试过用[self.superview.navigationController pushViewController:vc animated:YES];代替[self.navigationController pushViewController:vc animated:YES];吗?
  • @FabKremer 使用 self.superview.navigationController 获取 Property superview not found on object ListViewController
  • 对不起,@user1282637,我的意思是[self.parentViewController.navigationController pushViewController:vc animated:YES];

标签: ios objective-c uiviewcontroller uinavigationcontroller


【解决方案1】:

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

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

【讨论】:

    最近更新 更多