ios - 容器 View Controller addSubview 异常

标签 ios uiviewcontroller containers addsubview

我正在构建一个自定义容器 ViewController 来容纳一个 nib 的多个实例。这个 nib 包含一个子类为 DemoViewController 的 ViewController

在容器的 viewWillAppear 期间,我执行以下操作:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"Container will appear");


    if (_selectedViewController.parentViewController == self)
    {
        // nowthing to do
        return;
    }

    DemoViewController *vc = [[DemoViewController alloc] initWithNibName:@"Gauge" bundle:nil];

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"Gauge" owner:self options:nil];
    // assuming the view is the only top-level object in the nib file (besides File's Owner and First Responder)
    UIView *nibView = [nibObjects objectAtIndex:0];

    // add as child VC
    [self addChildViewController:vc];

    [view1 addSubview:nibView];

    // notify it that move is done
    [vc didMoveToParentViewController:self];

}

在运行期间,我收到以下异常:

2012-07-02 21:59:22.734 ContainerViewController[21747:f803] Loading Container View
2012-07-02 21:59:22.737 ContainerViewController[21747:f803] Container will appear
2012-07-02 21:59:22.740 ContainerViewController[21747:f803] Gauge View Loaded
2012-07-02 21:59:22.742 ContainerViewController[21747:f803] Gauge will move to parent controller
2012-07-02 21:59:22.743 ContainerViewController[21747:f803] -[DemoViewController superview]: unrecognized selector sent to instance 0x6a7daa0
2012-07-02 21:59:22.745 ContainerViewController[21747:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DemoViewController superview]: unrecognized selector sent to instance 0x6a7daa0'
*** First throw call stack:
(0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x4fe4f 0x4a14b 0x2d2c 0xdc38f 0xdc5eb 0xdd4ed 0x4aa0c 0x4ff92 0x4a14b 0x39550 0x39670 0x39836 0x4072a 0x11596 0x12274 0x21183 0x21c38 0x15634 0x12b3ef5 0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x11c65 0x13626 0x276d 0x26d5)
terminate called throwing an exception(lldb) 

有人知道这里发生了什么吗?不幸的是,关于容器 View Controller 的文档太少了,所以我很困惑。

最佳答案

顺便问一下,您为什么要尝试通过 NIB 检索 View ?为什么不是以下内容?

childController = [[FirstContainedViewController alloc] initWithNibName:@"FirstContainedView" bundle:nil];
[self addChildViewController:childController];
[self.view addSubview:childController.view];
[childController didMoveToParentViewController:self];

这对我有用,而且看起来简单多了。我一定不明白你为什么要这样做。

关于ios - 容器 View Controller addSubview 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11303563/

相关文章:

ios - 从 self.parentViewController 调用方法什么都不做

c++ - unordered_map.emplace 给出编译器时间错误?

docker - 更新正在运行的Docker容器

iphone - 检查 objective-c 枚举是否存在

ios - 在单触摸代码中从 AssetURL 创建 UIImage 时出现异常

ios - 从 SKScene 中解雇 MFMailComposeViewController

xcode - 无法将 View Controller 的框架添加到另一个 View Controller 中的 ScrollView

ios - 在 iOS 中返回 Viewcontroller 时如何释放 nsmutablearray

node.js - 如何使用在 docker 中运行的 Node 在微服务架构中初始化 postgres 数据库表

ios - UIControl Target Action 事件不会在响应者链中向上流动