我已经完成了这种授权模式一百万次。在百万分之一的时间内,我得到了零代表:

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的代表变为nil?

最佳答案

问题是CatViewController嵌入在情节提要中的导航控制器中。因此,尽管一切在屏幕上看起来都很好,但是InstantiateInitialViewController并未返回CatViewController。解决方法:

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

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

10-14 21:14