0

I have a view controller embeded in another view using 'addSubView' that is not catching events, how can I make sure it does?

background: I'm attempting to nest views in order to break up a storyboard that is to be shared by multiple devs. To accomplish this with minimal duplication of functionality I/we have created a mainStoryboard which contains a tab controller and 4 tabs, each tab contains a subview that loads a UIView (contained in another storyboard) into itself. These views are added like so:

//Add sub view
UIStoryboard *board = [UIStoryboard storyboardWithName:@"MessagesStory" bundle:nil];
UIViewController *boardController = [board instantiateInitialViewController];

[self.view addSubview:boardController.view];
boardController.view.frame = CGRectMake(0, 0, 320, 480);

The initial view controller that is loaded is a UITableView subclass, the whole thing works great for rendering the table and it's contents to the screen and I can interact with the table and select rows, however the event listener on the the views controller 'didSelectRowAtIndexPath' fails to fire.

I know it's not firing thanks to good ol' NSLog():

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Please kind sirs help a poor string appear in the console.");

}

I know it is related to the subview because if I load the subview on it's own as the main view the event listener functions properly.

Any help is appreciated as this is taking longer than expected and I may be pulled to another project before I can finish implementing this.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
wwwWRX
  • 23
  • 5
  • That is not the correct way to implement viewController containment – Paul.s Mar 22 '12 at 00:59
  • Okay thanks, can you point me in the right direction? – wwwWRX Mar 22 '12 at 01:02
  • If you have the time I would strongly recommend watching the [Apple video from wwdc 2011](https://developer.apple.com/videos/wwdc/2011/) look for the video/slides on `Implementing UIViewController Containment` – Paul.s Mar 22 '12 at 01:06
  • You should really read the docs: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457 By the way, I love the NSLog string!! – sosborn Mar 22 '12 at 01:08
  • Sounds like a problem that would be caused by the UITableView delegate not being set properly. – Patrick T Nelson Mar 22 '12 at 03:52

1 Answers1

1

I figured it out.

It was stunningly simple but I had to do quite a bit of reading to find the answer. All my searches where for 'addSubView' tutorials and examples I didn't even know 'addChildViewController' existed.

Anyway I believe it's this simple:

-(void)viewDidAppear:(BOOL)animated
{
    if (firstLaunch) {    
            firstLaunch = NO;

            //Find the view controller in the other storyboard
            UIStoryboard *board = [UIStoryboard storyboardWithName:@"MessagesStory" bundle:nil];
            UIViewController *boardController = [board instantiateInitialViewController];

            //add it as a child view controller (THIS IS WHAT I WAS MISSING)
            [self addChildViewController:boardController];

            //now it is okay to add the subview
            [self.view addSubview:boardController.view];

            //trigger this method (also missing this but it will run without it, I assume is good practice)
            [boardController didMoveToParentViewController:self];
    }

    [super viewDidAppear:animated];
}

Once I knew I wanted to 'addChildViewController' it was easy to find information:

How does View Controller Containment work in iOS 5?

Is it wise to "nest" UIViewControllers inside other UIViewControllers like you would UIViews?

Community
  • 1
  • 1
wwwWRX
  • 23
  • 5