0

In my XCode, by default, IBOutlets sets as weak. I read somewhere that Apple recommend to use strong outlets in the WWDC 2015 session Implementing UI Designs.

My question is, what is the need for using strong ?

What problem we will get if we use weak?

Why it is considered as best practice?

I saw duplicats of this questions. In all the places, they said what to use but didn't said why.

It would be great if you explain this with some example (Not just concept) so that i can understand better. Thanks for the time (:

Community
  • 1
  • 1
Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • There is no reason to use `strong` unless there will be no other `strong` reference. Apple still recommends using `weak` for IBOutlets. – Avi Jun 26 '16 at 06:25
  • Check these links https://twitter.com/_danielhall/status/620716996326350848 https://twitter.com/_danielhall/status/620716996326350848 – Ganesh Jun 26 '16 at 06:33
  • well i just clearly mentioned that i saw duplicates which doesn't answer this question. – Ganesh Jun 26 '16 at 07:09
  • `strong` ensures the object sticks around. If you declare a subview as `weak`, then remove it from its subview, it will get released and `nil` out. I remove and add activity indicators to and from views a lot, so I need it to be `strong`. When you connect an outlet, say a button, you're probably going to use it in your code. Setting `strong` ensures that as long as the view controller is around, the button will always be around for you to manipulate. Hence the best practice. The reason why `weak` is default for subviews is because at that moment, they're owned by their superview. – Kevin Low Jun 26 '16 at 07:35
  • @KevinLow I am opening new SinglePage application. Dragging an outlet say label and sets as weak. Here, it is the first view i am creating and hence it is not subview right ? So am i supposed to use strong here ? – Ganesh Jun 26 '16 at 07:40
  • Except you put the label inside the view controller's view, right? The label then is a subview of the view controller view. If you were to put the label outside the view controller's view (same level as First Responder and ViewController), then you'll notice it uses `strong`. – Kevin Low Jun 26 '16 at 07:43
  • Sorry, I'm bit confused. I am using storyboard... I created Viewcontorller and i dragged an outlet to viewcontroller.m file. Is it a View or SubView ? – Ganesh Jun 26 '16 at 07:47
  • A view is a subview if it's inside another view. When you create a Single View Application, the storyboard gives you the view controller's view (it's the white rectangle) to start off with. When you drag a label onto it, the label is now a subview of the view controller's view. That's also why it appears on the screen when you build and run. – Kevin Low Jun 26 '16 at 07:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115624/discussion-between-kevin-low-and-ganesh). – Kevin Low Jun 26 '16 at 08:01
  • Wow.. that's cool... So you are saying viewcontroller will have strong reference to subview(label) and hence we don't need to set subview as strong right? – Ganesh Jun 26 '16 at 08:17
  • Actually, the view controller has a `strong` reference to the full screen `UIView` that you put your label in. This full screen `UIView` then has a strong reference to this label. When you connect the label as a `strong` outlet to the ViewController.m, then both the view controller and the view controller's view would have a `strong` reference to the label. – Kevin Low Jun 26 '16 at 09:02

0 Answers0