xcode storyboard Container View - How do I access the viewcontroller

前端 未结 5 1942
温柔的废话
温柔的废话 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];
    

提交回复
热议问题