iOS self.delegate即使在设置后也是零

时间:2015-07-21 20:54:08

标签: ios objective-c uinavigationcontroller delegates viewcontroller

我有一个控制器,它被实例化并推送到导航控制器上,如下所示 -

在ParentVC.m

ThingContainerViewController *thingContainer = [[ThingContainerViewController alloc] init];
thingContainer.delegate = self;

[self.navigationController pushViewController:thingContainer animated:YES];

现在我想在关闭thingContainer时将值发送回ParentVC。所以我已经实现了如下代理:

在ParentVC.h中

@interface ParentVC : UIViewController <..., ThingContainerViewControllerDelegate>

在ThingContainer.h中

@protocol ThingContainerViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSInteger)page;
@end
....

@interface
....

@property (nonatomic, weak) id <ThingContainerViewControllerDelegate> delegate;

....
@end
在ParentVC.m中

- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSInteger)page {
    self.page = page;
}

在ThingContainer.m

- (IBOutlet) ... closeButtonWasTapped:(UIButton *)button {
    NSLog(@"%ld", (long)indexOfPage);
    [self.delegate addItemViewController:self didFinishEnteringItem:indexOfPage];
    [[self navigationController] popViewControllerAnimated: YES];
}

此时,self.delegate为null,因此永远不会调用ParentVC中的方法。知道如何解决它吗?

0 个答案:

没有答案