n-b / UIView-NibLoading

UIView category and subclass for loading content subviews from nib files.
http://bou.io/UIView-NibLoading.html
MIT License
67 stars 14 forks source link

Must remove from container's subviews when reparenting #1

Open Artmobile opened 11 years ago

Artmobile commented 11 years ago

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];
    }
}
n-b commented 11 years ago

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?

specialunderwear commented 11 years ago

I can confirm this is true

Artmobile commented 11 years ago

Pretty sure it's not. We are using ARC On Jun 9, 2013 4:54 PM, "Nicolas Bouilleaud" notifications@github.com 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 .

n-b commented 11 years ago

@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?

Artmobile commented 11 years ago

Sorry, closed by mistake

Artmobile commented 11 years ago

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?

specialunderwear commented 11 years ago

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

On 21 sep. 2013, at 07:22, IA notifications@github.com 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.

krusek commented 10 years ago

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

gslomin commented 10 years ago

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