Bigfoot11
Bigfoot11

Reputation: 921

UILabel returns "nil"?

I am having trouble with my UIButton. For Example, when I click the the UIButton Number3 and hit the checkButton on the ViewController the result in the currentCountLabel is Optional(3). It does this for every number after I click the checkButton. If I then click the button again, the currentCountLabel says, "nil". Here is all of my code:

ViewController.swift:

class ViewController: UIViewController {

var currentCountLabel: UILabel = UILabel()

var currentCount: Int = 0

var IncorrectLabel: UILabel = UILabel()

var CorrectLabel: UILabel = UILabel()

var currectAmountCorrect : Int = 0

var currentAmountIncorrect : Int = 0

var flashTimer : NSTimer = NSTimer()

var timerCounter : Int = 0

var randomImageGeneratorNumber : Int = 0

var flashingImageView: UIImageView = UIImageView()

var flashButton: UIButton = UIButton()

var currentTime = NSTimer()

func timerFunc() {
    flashButton.hidden = !flashButton.hidden
    flashingImageView.hidden = !flashingImageView.hidden
}

@IBAction func flashButton(sender: UIButton) {

    flashButton.hidden = !flashButton.hidden
    flashingImageView.hidden = !flashingImageView.hidden
    flashingImageView.image = UIImage(named: "Image\(arc4random_uniform(6) + 1).png")

    if (flashingImageView.image == UIImage(named: "Image1")){

        randomImageGeneratorNumber == 1

    }

    if (flashingImageView.image == UIImage(named: "Image2")){

        randomImageGeneratorNumber == 2

    }

    if (flashingImageView.image == UIImage(named: "Image3")){

        randomImageGeneratorNumber == 3

    }

    if (flashingImageView.image == UIImage(named: "Image4")){

        randomImageGeneratorNumber == 4

    }

    if (flashingImageView.image == UIImage(named: "Image5")){

        randomImageGeneratorNumber == 5

    }

    if (flashingImageView.image == UIImage(named: "Image6")){

        randomImageGeneratorNumber == 6

    }

    currentCount = 0
    currentCountLabel.text = "\(currentCount)"

    if (flashButton.hidden == true){
        flashButton.hidden = true;
        flashingImageView.hidden = false
    }

    var currentTime = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerFunc", userInfo: nil, repeats: false)

    func timerFunc(){
        self.flashingImageView.hidden = true;
        flashButton.hidden = !flashButton.hidden
        flashingImageView.hidden = !flashingImageView.hidden
    }

}

@IBAction func checkButton(sender: UIButton) {


    if (flashButton.hidden == true){
        flashButton.hidden == false
        flashingImageView.hidden == true
    }

    var currentCount = currentCountLabel.text?.toInt()
    if randomImageGeneratorNumber == currentCount {
        currectAmountCorrect += 1
        CorrectLabel.text = "\(currectAmountCorrect)"

    } else {
        currentAmountIncorrect += 1
        IncorrectLabel.text = "\(currentAmountIncorrect)"
    }

    if (currentCountLabel.text == "0"){

        let alert = UIAlertView()
        alert.title = "Alert"
        alert.message = "You must type in an answer in order to check it"
        alert.addButtonWithTitle("Understood")
        alert.show()

}
     currentCount == 0
     currentCountLabel.text = "\(currentCount)"
}


@IBAction func clearButton(sender: UIButton) {
    currentCount = 0
    currentCountLabel.text = "\(currentCount)"
}


@IBAction func Number1Button(sender: UIButton) {
    currentCount = currentCount * 10
    currentCount = currentCount + 1
    currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number2Button(sender: UIButton) {
    currentCount = currentCount * 10
    currentCount = currentCount + 2
    currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number3Button(sender: UIButton) {
    currentCount = currentCount * 10
    currentCount = currentCount + 3
    currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number4Button(sender: UIButton) {
    currentCount = currentCount * 10
    currentCount = currentCount + 4
    currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number5Button(sender: UIButton) {
    currentCount = currentCount * 10
    currentCount = currentCount + 5
    currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number6Button(sender: UIButton) {
    currentCount = currentCount * 10
    currentCount = currentCount + 6
    currentCountLabel.text = "\(currentCount)"
}


override func viewDidLoad() {
    super.viewDidLoad()
    var currentCountLabel = "\(currentCount)"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

I have no idea what so ever is happening or how to fix it.

Any input and suggestions would be greatly appreciated.

Thanks in advance.

Upvotes: 0

Views: 483

Answers (1)

Aaron
Aaron

Reputation: 6714

It would help if you clarified how you want it to behave. If you're wondering why it's an optional and why it's returning nil then you can fix this as follows.

In your checkButton() function you have this line at the end:

currentCountLabel.text = "\(currentCount)"

Unwrap the currentCount variable to this:

currentCountLabel.text = "\(currentCount!)"

Within this same checkButton() function you have this line:

currentCount == 0

So if you're wondering why it's not returning 0 it's because you're using a comparison operator not an assignment operator.

Upvotes: 1

Related Questions