I've seen a number of posts related to delegates, and I would like to know the proper way to reference them. Suppose I have an object declared like:
@interface MyViewController : UITableViewController {
id delegate;
}
@property (nonatomic, retain) id delegate;
@end
Through the lifecycle of MyViewController
, it will make calls to methods of its delegate in response to interaction with the user.
When it's time to get rid of an instance of MyViewController
, does the delegate
ivar need to be release
'ed in the implementation's dealloc
method since it is declared with retain
?
Or conversely, should delegate
even be retained? Perhaps it should be @property (nonatomic, assign) id delegate
? According to Apple's docs:
retain ... You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own such as delegates.
Normally I'd just go with what the docs say, but I've seen a lot of code that calls retain
on a delegate. Is this just "bad code?" I defer to the experts here... What is the proper way to handle this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…