Subviews added to a view are automatically retained by the view. Suppose you want to have a separate pointer to the same subview so you don’t need to constantly retrieve it via its tag.
What type of @property is necessary for such a case? I assume that setting the property to retain is not a good idea since the main view is already retaining it? Should it be assign?
Or, is using @property entirely unnecessary here unless you plan to re-assign it later or refer to it with dot notation?
You can use either
retainorassign.Of course, if you use
retain, you have to set the property tonilor release its object inviewDidUnloadanddealloc.The reason some people prefer
retainis because it means the property is still valid inviewDidUnload. So if you have other cleanup to do, and that cleanup requires the view to still exist, you can do it inviewDidUnload.If you use
assign, you don’t have to set the property tonilinviewDidUnloadanddealloc(though it would be good practice). However, by the time you receiveviewDidUnload, the view has already been released, so you can’t use it at that point for other cleanup. Instead you have to overridedidReceiveMemoryWarningto do the cleanup before calling[super didReceiveMemoryWarning].In iOS 5.0, you can do the cleanup in
viewWillUnloadinstead of overridingdidReceiveMemoryWarning.