Objective-C:如何在容器视图控制器中调用方法
从下面解决我的问题
与isKindOfClass。 感谢@ 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; } } }
///
我的问题
我正在使用故事板。 我读过容器视图的子视图控制器自动实例化。 如何从RedViewController调用我的BlueViewController中的方法? 我已经在这里尝试了几个解决scheme,但在我的情况下没有任何工作。
结构目前是:
EntryViewController.h / .M
..查看
….其他对象
….容器视图
……..容器视图RateViewController.h / .m
这是我的设置到目前为止。 我需要做什么。 我真的很想知道这是如何工作的:
/
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
您应该能够通过父级的childViewControllers
属性访问ViewController的子childViewControllers
。 (或者使用上面指出的segue)。
例如:
BlueViewController *bvc = self.childViewControllers[0]; //assuming you have only one child [bvc someMethod];
我通常在prepareForSegue方法中获取控制器对象。
这里是我使用的一些代码。 请记住,我对segues的命名约定总是类名+ Segue,所以ActionMenuVC的inheritance者是ActionMenuVCSegue。 这样保存才能抓取正确的视图控制器。
- (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; } }