简体   繁体   中英

My subViews are not being removed from SuperView Xcode7 Swift

I have a game that has a function that calls another SKScene. This SKScene is my gameOver scene. This game over scene has a UIButton and the UIButton is linked to a function that removes all of the UIButtons in that scene then calls the regular game scene again. If I run it maybe 3 or 4 times, the buttons will be removed and the game will start again with no problems. However on the 4th or 5th play, once I press the play again button, the buttons stay on the screen while the game is in the regular game screen class and do not have any functionality. What is going on here because the function is definitely being called because I printed something in the function. Can some one help with this problem.

    func playAgainAction(sender: UIControlEvents){
    removeGameOverButtons()
    let reveal : SKTransition = SKTransition.crossFadeWithDuration(0.5)
    let scene = GameScene(size: self.view!.bounds.size)
    scene.scaleMode = .AspectFill
    self.view?.presentScene(scene, transition: reveal)  
}

func removeGameOverButtons(){
    self.playAgainButton.removeFromSuperview()
    self.noAdsButton.removeFromSuperview()
    self.rateButton.removeFromSuperview()
    self.shareButton.removeFromSuperview()
    self.leaderboardsButton.removeFromSuperview()
    self.watchAdsButton.removeFromSuperview()
    print("There should be no reason that there labels are not being removed")

}
   self.rateButton = UIButton(type: UIButtonType.Custom)
    self.rateButton.setImage(rateButtonImage, forState: .Normal)
    self.rateButton.frame = CGRectMake(self.frame.size.width*3/4 - 26, self.frame.height*3/5 - 95 - 95, 110,75)
    self.rateButton.layer.anchorPoint = CGPointMake(1.0, 1.0)
    self.rateButton.layer.zPosition = 0
    //Attach an action to the play again button
    self.rateButton.addTarget(self, action: "noAdsAction:", forControlEvents: UIControlEvents.TouchUpInside)
    delay(0.5){
        view.addSubview(self.rateButton)
    }

Here is the copy of my code where I add the button. So it works the first couple of times through however, on time 3 or 4 they do not get removed. All My buttons on the game over are being added the exact same

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