Removing view from its superview

后端 未结 3 903
臣服心动
臣服心动 2021-01-25 12:09

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

         


        
相关标签:
3条回答
  • 2021-01-25 12:28

    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.

    0 讨论(0)
  • 2021-01-25 12:33

    Should this line:

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

    really be:

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

    ?

    0 讨论(0)
  • 2021-01-25 12:34

    Try removing the subview like this:

    for (UIView *subView in self.view.subviews) {
        if (subView.tag == (int)yourSubViewTag) {
    
            [subView removeFromSuperview];
        }
    }
    
    0 讨论(0)
提交回复
热议问题