0

在我的游戏应用程序中,我使用 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];

} 
4

1 回答 1

2

你需要掌握你的polygonView

-(void)buttonPressed {
    UIView *polygonView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [polygonView removeFromSubview];
}
于 2012-10-28T22:10:38.827 回答