gpt4 book ai didi

ios - 为什么我的委托(delegate)变成零?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:03:30 26 4
gpt4 key购买 nike

这个委托(delegate)模式我已经做过一百万次了。在第一百万次我得到一个零委托(delegate):

CatViewController *vc = [[UIStoryboard storyboardWithName:@"Cat" bundle:nil] instantiateInitialViewController];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:^{
DDLogWarn(@"[%@ %@] delegate: %@", THIS_FILE, THIS_METHOD, vc.delegate); //here the delegate is valid
}];

@protocol CatViewControllerDelegate;

@interface CatViewController : UIViewController

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

@end

@protocol CatViewControllerDelegate <NSObject>

@required
- (void)catViewController:(CatViewController*)catVC didFinishWithSuccess:(BOOL)success;

@end

但是,在 CatViewController 的 viewDidLoad 中,self.delegate 已经是 nil,当我尝试这样做时当然是 nil:

    [self.delegate catViewController:self didFinishWithSuccess:YES];

为什么 catViewController 的委托(delegate)变为 nil?

最佳答案

问题是 CatViewController 嵌入在 Storyboard 的 Navigation Controller 中。因此 instantiateInitialViewController 没有返回 CatViewController,尽管屏幕上的一切看起来都很好。修复:

UINavigationController *navVC = [[UIStoryboard storyboardWithName:@"Cat" bundle:nil] instantiateInitialViewController];
CatViewController *catVC = navVC.viewControllers[0];
catVC.delegate = self;

关于ios - 为什么我的委托(delegate)变成零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com