Denkir
Denkir

Reputation: 1

Application freezing when I press button

So, my assignment for my class is to make an app for the 7th graders at my school to work on math.

In this particular view controller, I am making something that they can practice prime numbers with. I couldn't get everything to load using the override func viewDidLoad(){} so I implemented a button to activate the random number, modulus to check if it's prime, and insert it into the label.

This is ALL of my code that is inside of the Class:

@IBAction func primeBack(sender: UIButton) {
    self.performSegueWithIdentifier("primeBack", sender: nil)
}
@IBOutlet var start: UIButton!

@IBOutlet var primeNum: UILabel!

    var num = 1
    var check = Double()
    var temp2 = Double()

    let lower : UInt32 = 1
    let upper : UInt32 = 100

override func viewDidLoad() {
    super.viewDidLoad()
    primeNum.hidden = true
    start.hidden = false

}

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

@IBAction func start(sender: UIButton) {

    primeNum.hidden = false

    let temp1 = (arc4random_uniform(upper - lower) + lower)
    primeNum.text = String(temp1)

    while num <= 12
    {
        check = Double(temp1) / Double(num)
        temp2 = check % 1

        if temp2 == 0
        {
            num++
        }
        else
        {
            num = 12
        }
    }
    start.hidden = true

}

@IBAction func prime(sender: UIButton) {
    if temp2 == 0
    {
        self.performSegueWithIdentifier("primeCorrect", sender: nil)
    }
    else
    {
        self.performSegueWithIdentifier("primeIncorrect", sender: nil)
    }
}
@IBAction func notPrime(sender: UIButton) {
    if temp2 == 0
    {
        self.performSegueWithIdentifier("primeIncorrect", sender: nil)
    }
    else
    {
        self.performSegueWithIdentifier("primeCorrect", sender: nil)
    }
}

Upvotes: 0

Views: 46

Answers (1)

Bradley Thomas
Bradley Thomas

Reputation: 4097

Debug and watch what happens to your value of temp2. I suspect it never becomes equal to zero, hence your while loop never ends

Upvotes: 2

Related Questions