如何隐藏在iphone应用程序中单击按钮时删除子视图

时间:2013-07-18 05:10:52

标签: iphone ios window subview

我有iphone应用程序,其中我将subView添加到窗口它工作正常但我希望当我按下关闭按钮它应该隐藏subView这里是我正在做的创建子视图的代码

 UIView*subView=[[UIView alloc]initWithFrame:CGRectMake(0,0, 1024,768)];
 subView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgPopupback.png"]];
 UIWindow* window = [UIApplication sharedApplication].keyWindow;
 if (!window) 
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
 [[[window subviews] objectAtIndex:0] addSubview:subView];  

关闭按钮操作

   -(void)closeButtonAction{



   NSLog(@"CLicked on this button");

  [subView removeFromSuperview] ; 

  self.tableView.userInteractionEnabled=TRUE;

  }

4 个答案:

答案 0 :(得分:9)

您可以使用subView删除removeFromSuperview,如果您只想隐藏子视图,请在按钮操作中使用subView.hidden = YES;

答案 1 :(得分:3)

删除您的观点

[yourView removeFromSuperview];  

隐藏您的观点

[yourView setHidden:YES];

答案 2 :(得分:1)

如果你只是想隐藏你的子视图,那么在你的 - (void)closeButtonAction方法中这样做

-(void)closeButtonAction{



   NSLog(@"CLicked on this button");

  //[subView removeFromSuperview] ; 

  subView.alpha = 0;

  self.tableView.userInteractionEnabled=TRUE;

  }

答案 3 :(得分:0)

viewDidLoad()
{
UIButton *m_btnSample = [UIButton buttonWithType:UIButtonTypeCustom];
    [m_btnSample setFrame:CGRectMake(200, 300, 200, 40)];
    [m_btnSample setImage:[UIImage imageNamed:@"smiley1.jpg"] forState:UIControlStateNormal];
    [m_btnSample addTarget:self action:@selector(btnChanged) forControlEvents:UIControlStateHighlighted];
    [self.view addSubview:m_btnSample];
}

 -(void)btnChanged
    {
        [viewYouWantToRemove removeFromSuperview];

    }