ContainerViewController委托给ChildViewController

时间:2015-05-11 04:15:08

标签: ios objective-c uiviewcontroller delegates

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!");
}

1 个答案:

答案 0 :(得分:3)

你永远不会设置委托,所以self.delegate将是零。你应该这样做,

{{1}}