容器视图控制器addSubview异常 [英] Container View Controller addSubview Exception

查看:87
本文介绍了容器视图控制器addSubview异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个自定义容器ViewController,以容纳笔尖的多个实例。此笔尖包含一个子类为DemoViewController的ViewController



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

 -(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@将出现容器);


if(_selectedViewController.parentViewController == self)
{
//现在要做
返回;
}

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

NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@ Gauge owner:self options:nil];
//假设视图是nib文件中唯一的顶级对象(文件的所有者和第一响应者除外)
UIView * nibView = [nibObjects objectAtIndex:0];

//添加为子级VC
[self addChildViewController:vc];

[view1 addSubview:nibView];

//通知其移动已完成
[vc didMoveToParentViewController:self];

}

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

 
2012-07-02 21:59:22.734 ContainerViewController [21747:f803]正在加载容器视图
2012-07-02 21:59 :22.737 ContainerViewController [21747:f803]容器将出现
2012-07-02 21:59:22.740 ContainerViewController [21747:f803]已加载量规视图
2012-07-02 21:59:22.742 ContainerViewController [ 21747:f803]标尺将移至父控制器
2012-07-02 21:59:22.743 ContainerViewController [21747:f803]-[DemoViewController超级视图]:无法识别的选择器已发送至实例0x6a7daa0
2012-07- 02 21:59:22.745 ContainerViewController [21747:f803] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[DemoViewController超级视图]:无法识别的选择器已发送至实例0x6a7daa0'
***首次抛出调用堆栈:
(0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x4fe4f 0x4a14b 0x2d2c 0xdc38f 0xdc5eb 0xdd4ed 0x4a a0c 0x4ff92 0x4a14b 0x39550 0x39670 0x39836 0x4072a 0x11596 0x12274 0x21183 0x21c38 0x15634 0x12b3ef5 0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x11c65 0bb)
$ b $ 276

任何人都知道这里发生了什么吗?不幸的是,关于Container View Controllers的文档很少,所以我很困惑。

解决方案

事实证明,通过解决我的问题的方法。



我最初是分别创建所有内容并将其链接在一起,方法是单击笔尖的框架并将其链接到视图控制器(就像在情节提要中一样)。事实证明,这是完全错误的。



相反,您需要将文件的所有者链接到ViewController文件。



感谢大家的帮助!


I'm building a Custom Container ViewController to house several instances of a nib. This nib holds a ViewController subclassed as DemoViewController

During the viewWillAppear of the Container, I perform the following:

- (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];

}

During runtime, I receive the following exception:

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) 

Anyone have any idea what's going on here? Unfortunately, there's so little documentation on Container View Controllers, so I'm stumped.

解决方案

It turns out, creating a nib by means of "Xcode's File->New->New File->Cocoa Touch->UIViewController subclass->with XIB for user interface" fixed my problem.

I was initially creating everything separately and linking them together by clicking on the frame of the nib and linking it to the view controller (like you would in storyboard). This turns out to be completely wrong.

Instead, you need to link the File's Owner to the ViewController file.

Thanks everyone for your help!

这篇关于容器视图控制器addSubview异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆