ContainerViewController
将方法-delegateMethod
委派给ChildViewController
。
但是在以下代码中,未调用-delegateMethod
。
我认为这是因为_childViewController
已发布。
我如何解决它使运行-delegateMethod
?
ContainerViewController.h
@protocol ContainerViewDelegate <NSObject>
- (void)delegateMethod;
@end
@interface ContainerViewController : UIViewController
@property (nonatomic, assign) id<ContainerViewDelegate> delegate;
@end
ContainerViewController.m
@interface ContainerViewController () {
ChildViewController *_childViewController;
}
//...
- (void)viewDidLoad
{
_childViewController = [[WeeklyViewController alloc]init];
[self addChildViewController:_childViewController];
[self.view addSubview:_childViewController.view];
[_childViewController didMoveToParentViewController:self];
}
- (void)buttonAction {
[self.delegate delegateMethod];
}
ChildViewController.m
@interface ChildViewController () <ContainerViewDelegate>
//...
- (void)delegateMethod {
NSLog(@"succeed!");
}
最佳答案
您永远不会设置委托,因此self.delegate将为nil。你应该做这个,
- (void)viewDidLoad
{
_childViewController = [[WeeklyViewController alloc]init];
self.delegate = _childViewController;
[self addChildViewController:_childViewController];
[self.view addSubview:_childViewController.view];
[_childViewController didMoveToParentViewController:self];
}
关于ios - ContainerViewController委托(delegate)给ChildViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30159578/