Best practice for custom UIView subview strong versus weak

So, I am writing a custom class that inherits from UIView. I have a bunch of subviews that I add.

So, work on two issues. IF I make supervisor and subroutine references strong, views leak. If I make them weak, they will not appear at all. What am I doing wrong?

CustomUIView

@interface CustomUIView : UIView @property(nonatomic, strong) AnotherCustomUIView *mySubView; @end @implementation CustomUIView { - (void)initCommon { self.mySubView = [self createSubView] } - (AnotherCustomUIView *) createSubView { return [[AnotherCustomUIView alloc] init:self]; } @end 

AnotherCustomUIView

 @interface AnotherCustomUIView : UIScrollView @property (nonatomic, strong) CustomUIView *ownerView; @end @implementation AnotherCustomUIView - (id)init:(CustomUIView *) ownerView { self = [super init]; if (self) { self.ownerView = ownerView; self.delegate = self; } return self; } @end 
+8
memory-management ios objective-c
source share
2 answers

Based on your code, I think you have a confusion between strong and weak links and how they relate to memory management.

Firstly, here is a great description of strengths versus weaknesses: https://stackoverflow.com/a/16578/

In your particular case, the ownerView property on AnotherCustomUIView should be weak, as it points to the senior object. It is not enough to delegate links, I do not know that setting self.delegate = self; has any negative effect.

Edit:

To clarify, adding a subtitle view of another view creates a strong link. Further strong references should not be necessary as soon as your presentation is part of the presentation hierarchy. So you have this code:

 [mySeniorView addSubView:myJuniorView]; // myJuniorView will not be released. mySeniorView.juniorView = myJuniorView; // This shouldn't be a strong reference. 

In the above code, if mySeniorView.juniorView is a strong link, it is redundant. If you remove myJuniorView from the view hierarchy, it will not be released because you have another strong link to it. If the .juniorView is a weak link, then removing myJuniorView from the view hierarchy will free it and set the .juniorView to nil .

This is why, for example, all of your IBOutlet properties must be weak; because the things in your xib with which you connect them are part of the hierarchy of views and therefore will not be dealloc, because their older views have strong references to them.

So, although I pointed out that your junior object should not have a strong reference to your senior object (none of them will be canceled, this is called a save cycle and will lead to a memory leak), probably your senior object should not have a strong reference to your youngest if you do not want it to hang, even if it is not in the hierarchy of views. After all, these are views; I would say create your junior object, put it in the hierarchy of views and then keep the reference to it in a weak property so that you can access it as long as it exists. Then, when you remove it from the view hierarchy, your link links are missing.

Anyway, hope this helps.

Link: Apple Docs, addSubview:

+9
source share

Almost always, the reference to the parent should be weak. This is a typical chain of commands. The parent creates and “owns” the child, and the child simply watches his parent. You want the child to be existentially dependent on the parent, and not vice versa.

+2
source share

All Articles