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.

Rajesh
  • 850
  • 9
  • 18
Gary Kagan
  • 139
  • 2
  • 7

7 Answers7

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.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
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];
    }
}
Prateek Prem
  • 1,544
  • 11
  • 14
0
  for (UIView *v in innerMainView.subviews)
    {
        [v removeFromSuperview];
    }   
    [innerMainView addSubview:StandardView];
Rinju Jain
  • 1,694
  • 1
  • 14
  • 23
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.

Siam
  • 109
  • 2
-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!

pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • 1
    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 weirdly – Chance Hudson Jun 15 '12 at 03:36
  • You're probably right. In fact it says something similar in the class reference. Thanks – pasawaya Jun 15 '12 at 03:49
  • 1
    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. – danh Jun 15 '12 at 04:27
  • Neither calling retain or switching the order of the statements has any effect. The new parent is definitely not nil and it is visible. – Gary Kagan Jun 15 '12 at 20:48
-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!

iWill
  • 135
  • 1
  • 4
-1

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

Prasad G
  • 6,702
  • 7
  • 42
  • 65