Search code examples
iphoneiosipaduiviewcontrolleruisplitviewcontroller

Set the Delegate on SubView


I have a splitViewController that has a master and detail view controllers. The code below is from the master and it creates the new view in the detail:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%@",[self.defaultSettingsMenuItems objectAtIndex:indexPath.row]]];

[self.detailViewController.view addSubview:controller.view];

detailViewController is a global instance of DetailViewController. In the detailViewController, I have many textFields and need to utilize the UITExtFieldDelegate. However, I think that the detailViewController isn't self at that point, and that's why I'm getting EXC_BAD_ACCESS errors on using the TextFieldDelegate methods in detailViewController.

EDIT: I have now found that the subView delegate methods only work for the viewController I setup as the rootViewCOntroller relationship from within Storyboard. Ex. If I have 6 views in the default menu settings above, whichever one I have setup as the first and root view in storyboard will work correctly. Any and all other subviews shown (from making a new selection in the master view) will not work properly. I think this will help diagnose the problem.


Solution

  • This all I needed, the second line:

    UIViewController *viewController= [self.detailViewController.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%@",[self.defaultSettingsMenuItems objectAtIndex:indexPath.row]]];
    
    if (self.detailViewController.childViewControllers.count >= 1) {
        NSLog(@"childViewControllers: %@",self.detailViewController.childViewControllers);
        [[self.detailViewController.childViewControllers objectAtIndex:0] removeFromParentViewController]; 
    
    }
    
    [self.detailViewController addChildViewController:viewController];
    
    [self.detailViewController.view addSubview:viewController.view];
    

    EDIT: I've updated my answer with the if look to remove viewControllers from the stack. Slightly hacky, but functional.