删除以编程方式创建的视图iOS

时间:2012-10-28 22:01:45

标签: ios button view

在我的游戏应用程序中,我使用uibutton以编程方式创建了一个视图。  通过单击按钮,将删除整个视图。但是我用“[polygonView removeFromSuperview];”删除视图的所有尝试都没有成功。例如。 我想我需要 首先获取当前视图,但不知道如何执行此操作并在中实现该视图 按钮方法。

这是代码:

- (void) addNewView {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 60, 0, 900, 900)];
    polygonView.backgroundColor = [UIColor blueColor];

    polygonView.tag = 42;
    // [self playMovie];

    [window addSubview:polygonView];

    // [polygonView setHidden:NO];

    [polygonView release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(100, 170, 100, 30);

    [button setTitle:@"Click Me!" forState:UIControlStateNormal]; 

    [button addTarget:self action:@selector(buttonPressed) 
     forControlEvents:UIControlEventTouchUpInside];


    [polygonView addSubview:button];

    // NSLog(@"mike=%@ tag=%d",[[polygonView class] description], [polygonView tag]);

}


-(void)buttonPressed {
    NSLog(@"Button Pressed!");
    [polygonView removeFromSuperview];

} 

1 个答案:

答案 0 :(得分:2)

您需要暂停polygonView

-(void)buttonPressed {
    UIView *polygonView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [polygonView removeFromSubview];
}