[英]UIImageView is not removed from Superview in ObjectiveC
So , i have this void where i initialize the GUI and remove it when it's clicked the play image i call it with [self GUI:1]; 所以,我在初始化GUI的地方有这个空白,当我单击使用[self GUI:1]调用的播放图像时,将其删除。 and [self GUI:0];
和[self GUI:0];
The GUI comes up but when i try to hide it it's not working but it's entering in the if() GUI出现了,但是当我尝试隐藏它时,它不起作用,但是正在if()中输入
-(void)GUI:(int)action{
// GUY LOADING
UIImageView *menu =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-350/2,self.view.center.y-300/2,350,200)];
UIImageView *menuplay =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-270/2,self.view.center.y-50/2,133,50)];
UIImageView *menuretry =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x+5,self.view.center.y-50/2,133,50)];
menu.image=[UIImage imageNamed:@"menustart.png"];
menuplay.image=[UIImage imageNamed:@"menuplay.png"];
menuretry.image=[UIImage imageNamed:@"retrymenu.png"];
if(action == 1){
[self.view addSubview:menu];
[self.view addSubview:menuplay];
[self.view addSubview:menuretry];
}
if(action == 0){
[menu removeFromSuperview];
[menuplay removeFromSuperview];
[menuretry removeFromSuperview];
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(StartGame:)];
singleTap.numberOfTapsRequired = 1;
[menuplay setUserInteractionEnabled:YES];
[menuplay addGestureRecognizer:singleTap];
}
the StartGame selector only executes the following code StartGame选择器仅执行以下代码
[self GUI:0];
and the viewDidLoad only executes the following code 并且viewDidLoad仅执行以下代码
[self GUI:1];
Every time the methode GUI:
is called, no it not called a void
you create new instance of the imageviews. 每次调用methode
GUI:
,不创建新的imageview实例,否则它不称为void
。 Also GUI
is not a good name for a method, better would be something like: setMenuVisible:
GUI
也不是方法的好名字,最好是这样的: setMenuVisible:
Since there is no reference to the old, previous image view they can not removed, You need to keep a reference to the image views. 由于没有对旧的,以前的图像视图的引用,因此无法删除它们,因此需要保留对图像视图的引用。
So in your header do the following: 因此,在标头中执行以下操作:
@property (strong, nonatomic) UIImageView *menu;
@property (strong, nonatomic) UIImageView *menuplay;
@property (strong, nonatomic) UIImageView *menuretry;
Then in you GUI:
methode: 然后在您的
GUI:
methode:
-(void)GUI:(int)action{
// GUY LOADING
if (!self.menu) {
self.menu =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-350/2,self.view.center.y-300/2,350,200)];
menu.image=[UIImage imageNamed:@"menustart"];
}
if (!self.menuplay) {
self.menuplay =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-270/2,self.view.center.y-50/2,133,50)];
menuplay.image=[UIImage imageNamed:@"menuplay"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(StartGame:)];
singleTap.numberOfTapsRequired = 1;
[menuplay setUserInteractionEnabled:YES];
[menuplay addGestureRecognizer:singleTap];
}
if (!self.menuretry) {
self.menuretry =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x+5,self.view.center.y-50/2,133,50)];
menuretry.image=[UIImage imageNamed:@"retrymenu"];
}
if(action == 1){
[self.view addSubview:self.menu];
[self.view addSubview:self.menuplay];
[self.view addSubview:self.menuretry];
}
else if(action == 0){
[self.menu removeFromSuperview];
[self.menuplay removeFromSuperview];
[self.menuretry removeFromSuperview];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.