Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c: How to invoke method in container view controller [duplicate]

Solution for my problem from below

with isKindOfClass. Thanks to @Julian!

-(void)callContainerViewController {
for (UIViewController *childViewController in [self childViewControllers])
{
    if ([childViewController isKindOfClass:[ContainerViewController class]])
    {
        //found container view controller
        ContainerViewController *cvc = (ContainerViewController *)childViewController;

        //do something with your container view viewcontroller
        [cvc callFunction];

        break;
    }
}
}

///

My Problem

I'm using storyboard. I've read that the child view controller of a container view is instantiated automatically. How do I call a method within my BlueViewController from the RedViewController? I've already tried several solutions here, but nothing worked in my case.

Structure is currently:

EntryViewController.h/.m

.. View

.... other Objects

.... Container View

........Container View RateViewController.h/.m

Here's my setup so far. What do I need to do. I really want to understand how this works:

/

EntryViewController.h

@interface EntryViewController : UIViewController {
}
@end

/

EntryViewController.m

#import RateViewController.h
@implementation

-(IBAction)callResetScrollViewMethodFromRateViewController {
[RateViewController resetScrollView];
}

@end

/

RateViewController.h

@interface RateViewController : UIViewController {
}
@property (nonatomic, assign) RateViewController *_RateViewControllerProperty;
@property (nonatomic, strong) IBOutlet UIScrollView *Scroller;
@end

/

RateViewController.m

@implementation

-(IBAction)resetScrollView {
[_Scroller setContentOffset:CGPointZero animated:NO];
}

@end
like image 303
Stephan Avatar asked Aug 19 '13 19:08

Stephan


2 Answers

You should be able to access the viewcontroller's child through parent's childViewControllers property. (Or using the segue as pointed out above).

Eg:

BlueViewController *bvc = self.childViewControllers[0]; //assuming you have only one child 
[bvc someMethod];
like image 142
Mario Avatar answered Oct 31 '22 17:10

Mario


I usually grab the controller object in the prepareForSegue method.

Here is some code I use. Remember that my naming convention for segues is always class name + Segue, so the segue for ActionMenuVC is ActionMenuVCSegue. This way is save to grab the right view controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:[self.appDelegate segueIdentifierForClass:[ActionMenuVC class]]]) {
        self.actionMenuVC = [segue destinationViewController];
        self.actionMenuVC.delegate = self;
    }
    else if ([[segue identifier] isEqualToString:[self.appDelegate segueIdentifierForClass:[ResizeableImageVC class]]]) {
        self.resizeableImageVC = [segue destinationViewController];
        self.resizeableImageVC.delegate = self;
        self.resizeableImageVC.visible  = NO;
   }
}
like image 34
Bernd Rabe Avatar answered Oct 31 '22 18:10

Bernd Rabe