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!");
}
答案 0 :(得分:3)
你永远不会设置委托,所以self.delegate将是零。你应该这样做,
{{1}}