简体   繁体   中英

Removing UIView from view

I have a UIView and a few labels and buttons which are built in a function menuState. When I click a button in the menuState function, it takes us out of the function into menuPlayButtonClicked() function. In this function, I'm trying to hide the labels and the UIView which I built in the menuState function because the game is beginning. Using menuView.isHidden = true is not doing it. menuView.removeFromSuperView() isn't doing it either. I tried declaring these views and labels globally with a let menuView = UIView() as well, but it still isn't removing it. What am I missing? I can still see the labels and UIView in the background underneath the game components.

func menuState() {

//Build the menu box
    let menuView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 400))
    menuView.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
    self.view.addSubview(menuView)
    menuView.layer.zPosition = 1;
    menuView.layer.cornerRadius = 10
    menuView.isHidden = false

...

Also some code for a button here, which takes us to the setupGame function when clicked

}

Your menuView object which is declared globally is not the same one which is created locally inside the function named "menuState()". So, don't declare another local instance, you may try following way -

menuState() {
  menuView = ...   // don't use `let` or 'var' again here, but reference the same global variable that you will use later to hide
}

The "menuView" in two functions is not the one object, just do not use "let" . By the way , if you will show menuView again in the future , use "isHidden" ,if you will never use it again , use "removeFromSuperView" to free your memory.

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