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;
}
}
}
///
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
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];
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With