xcode storyboard Container View - How do I access the viewcontroller

前端 未结 5 1918
温柔的废话
温柔的废话 2020-12-23 13:02

I\'m trying to use storyboard and get things working properly. I\'ve added a a Container View to one of my existing views. When I try to add a reference to this in my view c

相关标签:
5条回答
  • 2020-12-23 13:09

    The answer of Vitor Franchi is correct but could be more performant and convenient. Especially when accessing the child view controller several times.

    Create a readonly property

    @interface MyViewController ()
    @property (nonatomic, weak, readonly) InstallViewController *cachedInstallViewController;
    @end
    

    Then create a convenient getter method

    - (InstallViewController *)installViewController
    {
        if (_cachedInstallViewController) return _cachedInstallViewController;
    
        __block InstallViewController *blockInstallViewController = nil;
        NSArray *childViewControllers = self.childViewControllers;
        [childViewControllers enumerateObjectsUsingBlock:^(id childViewController, NSUInteger idx, BOOL *stop) {
    
            if ([childViewController isMemberOfClass:InstallViewController.class])
            {
                blockInstallViewController = childViewController;
                *stop = YES;
            }
        }];
    
        _cachedInstallViewController = blockInstallViewController;
    
        return _cachedInstallViewController;
    }
    

    From now on access the child view controller that way

    [self.installViewController doSomething];
    
    0 讨论(0)
  • 2020-12-23 13:15
    UIView* viewInsideOfContainer = installerView.subviews[0];
    

    Will give you the UIView inside of the UIViewController that your controller UIView references. You can cast the subview to any type that inherits from UIView.

    0 讨论(0)
  • 2020-12-23 13:25

    If the nib is loaded it will call addChildViewController as part of the initialisation process

    so a performant solution could be also to overwrite

    - (void)addChildViewController:(UIViewController *)childController
    

    there you can catch your childController e.g. by comparing its Class and assign it to a property / ivar

    -(void)addChildViewController:(UIViewController *)childController
    {
        [super addChildViewController:childController];
    
        if([childController isKindOfClass:[InstallViewController class]])
        {
            self.installViewController = (InstallViewController *)childController;
        }
    

    }

    This will save your from iterating trough the childViewControllers.

    0 讨论(0)
  • 2020-12-23 13:27

    When you add a container view the xcode calls the UIViewController method addChildViewController:

    In your case, you can get the container ViewController looking for it on the SplashViewController's list of childViewControllers, something like this:

    for (UIViewController *childViewController in [self childViewControllers])
    {
        if ([childViewController isKindOfClass:[InstallViewController class]])
        {
            //found container view controller
            InstallViewController *installViewController = (InstallViewController *)childViewController;
    
            //do something with your container view viewcontroller
    
            break;
        }
    }
    

    I had the same doubt yesterday :)

    0 讨论(0)
  • 2020-12-23 13:30

    There is another solution by specifying an identifier for the embed segue(s) and retrieve the corresponding view controllers in method prepareForSegue:

    The advantage of this way is that you needn't rely on a specific order in which your child view controllers are added due to the fact that each child view controller is embedded via an unique segue identifier.

    Update 2013-01-17 - Example

    - (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
    {
        // -- Master View Controller
        if ([segue.identifier isEqualToString:c_SegueIdEmbedMasterVC])
        {
            self.masterViewController = segue.destinationViewController;
            // ...
        }
        // -- Detail View Controller
        else if ([segue.identifier isEqualToString:c_SegueIdEmbedDetailVC])
        {
            self.detailViewController = segue.destinationViewController;
            // ...
        }
    }
    

    c_SegueIdEmbedMasterVC & c_SegueIdEmbedDetailVC are constants with the corresponding ID of the segue IDs defined in the storyboard.

    0 讨论(0)
提交回复
热议问题