Remove a view from one view and add it to another
Asked Answered
C

7

6

I am trying to find all my views that are in the nib and add them to my content view.

This is what I have, It successfully removes the view from self.view but it does not add it to self.contentView

for (UIView *view in self.view.subviews) {
    if (view.tag != 666) {
        [view removeFromSuperview];
        [self.contentView addSubview:view];
    }
}

Any help would be appreciated.

Catbird answered 15/6, 2012 at 3:24 Comment(3)
Did you check that self.contentView is linked to nib and is not nil?Cladoceran
self.contentView is inherited. It does not originate from a nib.Catbird
On this old question, the only correct answer here is the answer by MidhunMP. The rest are remarkably wrong / dangerous.Armhole
A
7

The issue in your code is, when you call removeFromSuperview the view will be released by the parent view. No need of calling removeFromSuperview, just add it as subview of another view will remove it from it's current parentView.

So use:

for (UIView *view in self.view.subviews)
{
    if (view.tag != 666)
    {
        [self.contentView addSubview:view];
    }
}

According to UIView Class Reference:

addSubview:

Adds a view to the end of the receiver’s list of subviews.

- (void)addSubview:(UIView *)view

Parameters

view

The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews. 

Discussion

This method retains view and sets its next responder to the receiver, which is its new superview.

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

Adrial answered 8/6, 2013 at 14:51 Comment(0)
G
1

When you remove your view from superView It will release from memory. So in your case you need to do this:

UINib *infoNib = [UINib nibWithNibName:@"YourXIBFileName" bundle:nil];

NSArray *topLevelObjects = [infoNib instantiateWithOwner:self options:nil];

UIView *infoView = [topLevelObjects objectAtIndex:0];
for (UIView *view in infoView.subviews) {
        [self.contentView addSubview:view];
    }
}
Grantland answered 8/6, 2013 at 17:23 Comment(0)
B
0
  for (UIView *v in innerMainView.subviews)
    {
        [v removeFromSuperview];
    }   
    [innerMainView addSubview:StandardView];
Batey answered 15/6, 2012 at 5:50 Comment(0)
A
0

You can set the specified view as a property, or you can add the view to the content view before removing it from the super view. I'm not sure when you remove the view, if the view's retain count is decreased to 0, it will call the super view to dealloc it.

Aeriform answered 15/6, 2012 at 9:27 Comment(1)
The retain count is still 1 after I remove it from the superView.Catbird
O
-1

I'm not sure if this would work, but try switching [view removeFromSuperview]; and [self.contentView addSubview:view];. This is because according to the UIView Class Reference, removeFromSuperview causes the superview to release the view.

Hope this helps!

Outflow answered 15/6, 2012 at 3:34 Comment(4)
Instead of this i would recommend doing [view retain]; [view removeFromSuperview]; [self.contentView addSubview:view]; [view release]; Just because calling removeFromSuperview when the view has 2 superviews could potentially end weirdlyPathy
You're probably right. In fact it says something similar in the class reference. ThanksOutflow
Instead of either, how about just comment out the remove. Adding to a new superview overwrites the superview property. But in all likelihood, the problem is because the new parent is either nil or hidden for some other reason.Collectivize
Neither calling retain or switching the order of the statements has any effect. The new parent is definitely not nil and it is visible.Catbird
H
-1

To check the errors. You'd better print the frames of these views (self.view, self.contentView). and make them different colors. Then you can see the bugs. Good Luck!

Heller answered 15/6, 2012 at 3:46 Comment(0)
B
-1

contentView doesn't belong to property of UIVew. It belongs to property of UITableViewCell. It returns the content view of the cell object.

Backstop answered 15/6, 2012 at 5:23 Comment(1)
This is not a UITableViewCell I created the contentView on my own it is not inherited.Catbird

© 2022 - 2024 — McMap. All rights reserved.