How to know if ios delegate is released
I use delegation extensively for VIEW - CONTROL - MODEL structure. Usually, CONTROL & MODEL objects are retained during the running of the app, causing little issues.
However, VIEW objects, which are often the instances of UIViewController
, are allocated and release numerous times.
Setting nil
to the delegate object who is assigned with dynamic UIViewController
instance, when it's being deallocated, is one way I know how to avoid EXC_BAD_ACCESS error.
However, even if I always use:
if ([delegate respondsToSelector:@selector(communityModel:finishedDeletingComment:)]) {
[delegate communityModel:self finishedDeletingComment:succeeded];
}
it's not perfect for preventing error. If different VIEW objects are sharing one or limited number of delegates of a CONTROL object, it's always prone to cause delegates to be replaced by another unintentionally, causing EXC_BAD_ACCESS
Is there better way to manage delegates, to know if it's really available at the moment of calling delegate methods?
I doubt if my usual way of setting properties for delegate sho开发者_如何学编程uld be changed. I use this way to set delegate's properties:
@property (nonatomic, assign) id<ControlDelegate> delegate;
Am I going to the right track doubting about property setting?
I declare delegates as assign
properties when my object (which will use delegate) is a member of delegate or member of member of delegate etc. In this case retain
property will lead to memory leak.
Elsewhere I do not see the reasons to not retains delegate. If it's acceptable for you - try to declare your property as:
@property (nonatomic, retain) id<ControlDelegate> delegate;
精彩评论