Objective-c:如何在容器视图控制器中调用方法 [英] Objective-c: How to invoke method in container view controller
问题描述
。感谢@Julian!
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;
}
}
}
///
我正在使用故事板。我已经读过容器视图的子视图控制器是自动实例化的。如何从RedViewController调用BlueViewController中的方法?我已经尝试了几种解决方案,但在我的情况下没有任何效果。
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.
结构目前:
EntryViewController.h / .m
..查看
....其他对象
....容器视图
........容器视图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
推荐答案
您应该能够通过父级的 childViewControllers
属性访问viewcontroller的子级。 (或者如上所述使用segue)。
You should be able to access the viewcontroller's child through parent's childViewControllers
property. (Or using the segue as pointed out above).
例如:
BlueViewController *bvc = self.childViewControllers[0]; //assuming you have only one child
[bvc someMethod];
这篇关于Objective-c:如何在容器视图控制器中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!