Question

I have read several Q&A and documentations which state that we should use weak for IBOutlet unless it's top level objects from File's Owner.

But if I still use strong/retained, is there any major downside, or is it just redundant because the subview is already retained with addSubview:?

Note: please do not copy definition of weak / strong here, I don't need that, I want to see real world cases where using strong for IBOutlet could cause problems. Thanks.

Was it helpful?

Solution 2

I will mark this as "accepted" until someone provides a better answer.

Apparently the only downside is that when your view receives a memory warning, it would unload the view, and optimally all the subviews should be released. But since your controller still retains them if you use strong, you will have to nil them out manually in viewDidUnload.

From iOS 6, view is not unloaded upon receiving memory warning, so this becomes inconsequential. From a practical point of view there is no major difference between using weak or strong for IBOutlet afaik, unless you have to unload your view manually in your application.

OTHER TIPS

With MRC, if you use retain, you will have to release the memory by yourself.

With ARC, if you use strong and the system requests memory from your app (= your view will be unloaded), you will have to release the memory by yourself (note that the controller would be still be active, so no dealloc called there)

For most outlets, weak/assign is appropiate because you don't need to care about releasing the memory.

Exceptions:

  1. IBOutletCollection must be strong/retain. The collection (NSArray) is not retained by the view hierarchy.

  2. You add/remove views dynamically. If you want to remove a view from your view hierarchy and use it again later, the view must be retained somewhere, otherwise it gets deallocated at the time of removal. However, note that you can always retain it in code at the time of removal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top