在我的游戏应用程序中,我使用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];
}
最佳答案
您需要保留您的polygonView
:
-(void)buttonPressed {
UIView *polygonView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
[polygonView removeFromSubview];
}