Container View Controller addSubview Exception

雨燕双飞 提交于 2019-12-25 05:00:37

问题


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.


回答1:


By the way, why are you trying to retrieve the view via the NIB at all? Why not the following?

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

This works for me and seems much simpler. I must not understand why you're doing what you're doing.




回答2:


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!




回答3:


Your NSLog() illustrates the problem perfectly. The top level object of that XIB is obviously not a UIView, it's most likely the file's owner object, which points to DemoViewController. In calling the method, you've sent -addSubview to a View Controller.



来源:https://stackoverflow.com/questions/11303563/container-view-controller-addsubview-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!