UCImageView不会从ObjectiveC中的Superview中删除

时间:2014-10-31 13:13:49

标签: ios objective-c uiimageview

所以,我有这个空白,我初始化GUI并在点击播放图像时删除它我用[self GUI:1]调用它;和[自我GUI:0];

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];

}

StartGame选择器仅执行以下代码

[self GUI:0];

并且viewDidLoad仅执行以下代码

[self GUI:1];

1 个答案:

答案 0 :(得分:3)

每次调用方法GUI:时,不会将其称为void,而是创建新的imageview实例。另外GUI不是方法的好名字,更好的是:setMenuVisible:

由于没有引用旧的,先前的图像视图,因此无法删除它们。您需要保留对图像视图的引用。

因此,在标题中执行以下操作:

@property (strong, nonatomic) UIImageView *menu;
@property (strong, nonatomic) UIImageView *menuplay;
@property (strong, nonatomic) UIImageView *menuretry;

然后在你GUI:方法:

-(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];
    }
}