tanman
tanman

Reputation: 175

Why is the button not working

The "go button" is not working as it should. When clicked it should hide the subviews. Here's the ViewController. I tried placing the final function in various places.

class timeViewController: UIViewController {


var score = 0
var buttonState: Int = 0;
var gameOver = UIView()

@IBAction func start(sender: AnyObject) {

  if(displayTime.text != stoptimer.text){

        var gameOver = UIView(frame: CGRectMake(0, 0, 0, 0))
        gameOver.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.2)
        //gameOver.opaque = false
        self.view.addSubview(gameOver)
        UIView.animateWithDuration(0, animations:{
            gameOver.frame.size = CGSizeMake(667, 667)
        })

        let gobutton = UIButton()
        let image = "button.png"
        gobutton.setImage(UIImage(named: image), forState: .Normal)
        gobutton.frame = CGRectMake(135, 300, 95, 95)
        gobutton.addTarget(self, action: "pressed:" , forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(gobutton)

        func pressed(sender: UIButton!){
             gameOver.removeFromSuperview()
        }
  }
}

Upvotes: 1

Views: 92

Answers (3)

JustAnotherCoder
JustAnotherCoder

Reputation: 2575

You're re-declaring gameOver inside the if statement. Just remove var. Check this code out:

class ViewController: UIViewController {

var gameOver: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func start(sender: AnyObject) {

    if(displayTime.text != stoptimer.text){

        gameOver = UIView(frame: CGRectMake(0, 0, 0, 0))
        gameOver.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.2)
        //gameOver.opaque = false
        self.view.addSubview(gameOver)
        UIView.animateWithDuration(0, animations:{
            self.gameOver.frame.size = CGSizeMake(667, 667)
        })

        let gobutton = UIButton()
        let image = "button.png"
        gobutton.setImage(UIImage(named: image), forState: .Normal)
        gobutton.frame = CGRectMake(135, 300, 95, 95)
        gobutton.addTarget(self, action: "pressed:" , forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(gobutton)

    }
}

func pressed(sender: UIButton!){
    self.gameOver.removeFromSuperview()
}
}

Upvotes: 0

LinusG.
LinusG.

Reputation: 28892

You will have to make gameOver a global variable by declaring it outside function and then do what you did like this:

class myClass {
    var gameOver = UIView()

    func myFunction() {/*edit everything including gameOverView's properties*/}
    func pressed(sender: UIButton!) {
        gameOver.removeFromSuperview()
    }
}

Also make sure to write functions outside other functions like viewDidLoad but inside your class as it looks like you created that function inside another one. However you can call a function from within another one using something like myFunction() as separate line as is.

Hope that helps :)

Upvotes: 1

Sohel L.
Sohel L.

Reputation: 9540

For removing your "GaneOver" view change to this code:

gameOver.removeFromSuperview()

And I hope you are writing "func pressed" outside of viewDidLoad or viewWillAppear

Upvotes: 1

Related Questions