UIView-NibLoading icon indicating copy to clipboard operation
UIView-NibLoading copied to clipboard

Must remove from container's subviews when reparenting

Open Artmobile opened this issue 11 years ago • 9 comments

I spent several hours trying to understand why a view that was created using your class crashed my application when I called removeFromSuperview on it. Turned out that you do not detach elements from the container when reparenting. To fix, just call [view removeFromSuperview ]; before reparenting.

- (void) loadContentsFromNibNamed:(NSString*)nibName
{
 ...
    // reparent the subviews from the nib file
    for (UIView * view in containerView.subviews){

        // Without this line,  will crash if you attempt calling removeFromSuperview
        [view removeFromSuperview ];

        [self addSubview:view];
    }
}

Artmobile avatar Jun 08 '13 11:06 Artmobile

This is quite surprising, I'm pretty sure addSubview effectively removes any link to the previous parent. Are you sure it's not a weird retain/release issue?

n-b avatar Jun 09 '13 13:06 n-b

I can confirm this is true

specialunderwear avatar Sep 19 '13 09:09 specialunderwear

Pretty sure it's not. We are using ARC On Jun 9, 2013 4:54 PM, "Nicolas Bouilleaud" [email protected] wrote:

This is quite surprising, I'm pretty sure addSubview effectively removes any link to the previous parent. Are you sure it's not a weird retain/release issue?

— Reply to this email directly or view it on GitHubhttps://github.com/n-b/UIView-NibLoading/issues/1#issuecomment-19166451 .

Artmobile avatar Sep 20 '13 19:09 Artmobile

@specialunderwear I've reread your original message and I'm even more puzzled. When does the crash happen exactly?

If I understand correctly, the scenario is:

  1. Load a view contents via NibLoading
  2. Later, send - removeFromSuperview to this view.

You see a crash at 2., is this it? Can you try to isolate the crash on a minimal test project?

n-b avatar Sep 20 '13 20:09 n-b

Sorry, closed by mistake

Artmobile avatar Sep 21 '13 05:09 Artmobile

I realize that addSubView should move the view from one parent to another. Still, it either did not happen or something else went on. @specialunderwear - are still experiencing the same problem?

Artmobile avatar Sep 21 '13 05:09 Artmobile

Yes, I fixed it in my fork. Crashes everytime ...

On 21 sep. 2013, at 07:22, IA [email protected] wrote:

I realize that addSubView should move the view from one parent to another. Still, it either did not happen or something else went on. @specialunderwear - are still experiencing the same problem?

— Reply to this email directly or view it on GitHub.

specialunderwear avatar Sep 21 '13 11:09 specialunderwear

I have the same issue (iOS 6 only) and the same fix works.

krusek avatar Jun 03 '14 18:06 krusek

I just had to fix this issue myself and was about to send a pull request. Definitely confirmed in iOS6. Works fine in 7 and 8

gslomin avatar Jun 20 '14 01:06 gslomin