Illep
Illep

Reputation: 16841

Removing view from its superview

I have added a few buttons to self.view. When the user clicks on one button, i will load another view (subView). My code where i am loading the subView is shown below

subView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.contentSize.width, self.scrollView.contentSize.height)];
subView.tag=1;
// and then i add various other components to it

[self.view addSubview:subView]; // and finally add it to the view

When the user touches this view, i detect it and remove the view (i have already done the detection part).

This is how i am removing the view

UIView *v = [self.subView viewWithTag:1];
    v.hidden = YES;
    [v endEditing:YES];
 [v removeFromSuperview];

The view dissapears, but i can't click on the buttons or anything that was on the self.view. When i hold the mouse cursor on a button for awhile i could click it otherwise i can't. How can i solve this ?

Upvotes: 1

Views: 3684

Answers (3)

Vincent Osinga
Vincent Osinga

Reputation: 1039

I think your problem is that you do

[self.subView viewWithTag:1];

but the view you want to remove is "owned" by self.view so it should be

[self.view viewWithTag:1];

But if you have a reference to subView, why search it again? Why not immediately:

self.subView.hidden = YES;
[self.subView endEditing:YES];
[self.subView removeFromSuperview];

Btw you might want to look into your memory management, I see you alloc your subView but I don't see it being released, when the view is removed again. Maybe you do and you just don't show the code, in that case forget my comment.

Upvotes: 1

Frade
Frade

Reputation: 2988

Try removing the subview like this:

for (UIView *subView in self.view.subviews) {
    if (subView.tag == (int)yourSubViewTag) {

        [subView removeFromSuperview];
    }
}

Upvotes: 3

Dancreek
Dancreek

Reputation: 9544

Should this line:

UIView *v = [self.subView viewWithTag:1];

really be:

UIView *v = [self.view viewWithTag:1];

?

Upvotes: 2

Related Questions