Delegate method not being called?

前端 未结 1 912
清歌不尽
清歌不尽 2021-02-06 04:48

I have a view controller with a delegate method that should be called, but it doesn\'t?

NotifyingViewController.h

@protocol NotifyingViewControllerDeleg         


        
相关标签:
1条回答
  • 2021-02-06 05:00

    You can't just specify that an object conforms to a protocol. You must also assign that object as the delegate. When you alloc/init the instance of NotifyingViewController, set its delegate to self and you should be fine.

    NotifyingViewController *notifyingInstance = [[NotifyingViewController alloc] init];
    [notifyingInstance setDelegate:self];
    

    It is important to both do this, and specify that the class conforms to the protocol, which you're already doing with this line.

    @interface NotifiedViewController : UIViewController <NotifyingViewControllerDelegate>
    

    Additionally, when calling delegate methods, it's good practice to wrap the function calls in respondsToSelector: checks.

    if ([self.delegate respondsToSelector:@selector(iWasAccepted)]) {
        [self.delegate iWasAccepted];
    }
    
    0 讨论(0)
提交回复
热议问题