iOS:如何从子视图中删除叠加层?

时间:2011-04-22 21:37:21

标签: iphone ios ios4 uiview

我有按钮,使用标签和X按钮覆盖:

- (IBAction)taptaxi:(id)sender {
    UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    overlay.backgroundColor = [UIColor colorWithWhite:0 alpha:.7];
    UILabel *waiting = [[[UILabel alloc] initWithFrame:CGRectMake(50, 210, 250, 30)] autorelease];
    waiting.text = @"Waiting for cab response...";
    waiting.textColor = [UIColor whiteColor];
    waiting.backgroundColor = [UIColor clearColor];   
    waiting.font = [UIFont systemFontOfSize:14];

    UIButton *stoprequest = [[UIButton alloc] initWithFrame:CGRectMake(225, 219, 13, 13)];
    UIImage *srbackground = [[UIImage imageNamed:@"iks.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
    [stoprequest setBackgroundImage:srbackground forState:UIControlStateNormal];
    [stoprequest addTarget: self action: @selector(stopRequestMethod:) forControlEvents: UIControlEventTouchUpInside];

    [self.view.window addSubview:overlay];
    [overlay addSubview:waiting];
    [overlay addSubview:stoprequest];


}

- (void)stopRequestMethod: (id)sender
{
}

问题,当点击X butoon(stopRequestMethod)时如何隐藏/删除带标签和按钮的叠加层?

1 个答案:

答案 0 :(得分:2)

为您的叠加层添加标签:

overlay.tag = 42;

然后,在按下X按钮时调用的方法中:

UIView *overlay = [self.view.window viewWithTag:42];
[overlay removeFromSuperview];