gpt4 book ai didi

ios - 容器 View Controller addSubview 异常

转载 作者:行者123 更新时间:2023-11-28 19:17:15 25 4
gpt4 key购买 nike

我正在构建一个自定义容器 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 View2012-07-02 21:59:22.737 ContainerViewController[21747:f803] Container will appear2012-07-02 21:59:22.740 ContainerViewController[21747:f803] Gauge View Loaded2012-07-02 21:59:22.742 ContainerViewController[21747:f803] Gauge will move to parent controller2012-07-02 21:59:22.743 ContainerViewController[21747:f803] -[DemoViewController superview]: unrecognized selector sent to instance 0x6a7daa02012-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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com