handle when callback to a dealloced delegate?

前端 未结 2 1510
栀梦
栀梦 2021-01-07 06:38

I implemented the delegate-callback pattern between two classes without retaining the delegate. But in some cases, the delegate is dealloced.

(My case is that I hav

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 07:02

    I have no objective-c knowledge, but I would think that you need to test for self.delegate != nil separately e.g

    if (self.delegate != nil)
    {
     if ( [self.delegate respondsToSelector:selector]) 
     {
        [self.delegate performSelector:selector withObject:self withObject:returnObject];
     }
    }
    

    Oh and it's best to reassign the delegate if it is nil.

提交回复
热议问题