Reputation: 421
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
}
Upvotes: 0
Views: 70
Reputation: 11
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.
Upvotes: 1
Reputation: 3052
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
}
Upvotes: 2