简体   繁体   中英

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]; and [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()

-(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

[self GUI:0];

and the viewDidLoad only executes the following code

[self GUI:1];

Every time the methode GUI: is called, no it not called a void you create new instance of the imageviews. Also GUI is not a good name for a method, better would be something like: 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM