9

从下面解决我的问题

与 isKindOfClass。感谢@朱利安!

-(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 中的方法?我已经在这里尝试了几种解决方案,但在我的情况下没有任何效果。

目前的结构是:

入口视图控制器.h/.m

.. 看法

.... 其他对象

.... 容器视图

........容器视图 RateViewController.h/.m

到目前为止,这是我的设置。我需要做什么。我真的很想了解这是如何工作的:

/

入口视图控制器.h

@interface EntryViewController : UIViewController {
}
@end

/

入口视图控制器.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
4

2 回答 2

7

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];
于 2013-08-19T21:24:24.313 回答
2

我通常在 prepareForSegue 方法中抓取控制器对象。

这是我使用的一些代码。请记住,我对 segue 的命名约定始终是类名 + Segue,因此 ActionMenuVC 的 segue 是 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;
   }
}
于 2013-08-19T19:58:00.837 回答