McDermott
McDermott

Reputation: 1035

iOS removing view

I have two views, viewA and viewB. I load viewB on top of viewA with

[self.view addSubview: viewB.view];

I wan't to remove viewB, but I don't how to do it. I tried

[self.view removeFromSuperview];

but this isn't working. How can I do this?

Upvotes: 11

Views: 20542

Answers (3)

user523234
user523234

Reputation: 14834

You are on the right track by using the removeFromSuperView. But you need to send the message to the view that you want to remove. Just as Till example

[viewB.view removeFromSuperview];

However, you might not have a handle to viewB by the time you want to remove it if you are not using property and synthesize method. I would make use of @property and @synthesize. So you can use:

[self.viewB.view removeFromSuperview];

Another way is using this: (assuming that your viewB.view is the last view you added to viewA.view

[[self.view.subviews objectAtIndex:(self.view.subviews.count - 1)]removeFromSuperview];

You can get a list of all subviews of your viewA by:

NSLog(@"subviews of viewA.view: %@",self.view.subviews);

Upvotes: 3

Till
Till

Reputation: 27587

To remove viewB's view from its superview, you need to call removeFromSuperview on that view.

[viewB.view removeFromSuperview];

From the UIView class reference.

removeFromSuperview

Unlinks the receiver from its superview and its window, and removes it from the responder chain.

Upvotes: 7

Sean
Sean

Reputation: 5820

Call -removeFromSuperview on viewB.view.

Upvotes: 19

Related Questions