mercredi 7 octobre 2015

Not able to get correct method for if statement and uialert in swift

I am under one project like finding quiz answer.It will randomely generate question with 3 option.if answer is correct score will be 1. if answer is not correct score will be decrease. And if core lessthan equal to 0 then an uialert will display "you Loss"

Problem:

But unfortunately if i click the correct answer also uialert is sowing "You loss". And if my answer is correct also my score is not incrementing.This is my viewcontroller.swift i declare so if statements also.i don't know still i miss anything wrong

import GameplayKit
import UIKit



class ViewController: UIViewController {
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!

    @IBOutlet weak var title2: UILabel!

    @IBOutlet weak var titleL: UILabel!
    @IBOutlet weak var scoreB: UITextField!
    var countries = [String]()
    var correctAnswer = 0
    var score = 0

    override func viewDidLoad() {
        super.viewDidLoad()

       // array to store answer .like this totally 30 answer in this answer array. 

        answers += ["answer1","answer 2","answer3",]


        askQuestion(nil)
    }

    func askQuestion(action: UIAlertAction!) {
        answers = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(answers) as! [String]

        button1.setImage(UIImage(named: countries[0]), forState: .Normal)
        button2.setImage(UIImage(named: countries[1]), forState: .Normal)
        button3.setImage(UIImage(named: countries[2]), forState: .Normal)

        correctAnswer = GKRandomSource.sharedRandom().nextIntWithUpperBound(3)
       self.titleL?.text = answers[correctAnswer].lowercaseString
    }

    @IBAction func buttonTapped(sender: UIButton) {
        var title: String

        if sender.tag == correctAnswer {
            title = "Correct"
            ++score
        } else {
            title = "Wrong"
            --score
        }





        if score <= 0 {

            scoreB.text = ""
            let ac = UIAlertController(title: "YOU LOSS" , message: "Your Score is 0", preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "Try Again", style: .Default, handler: askQuestion))
            presentViewController(ac, animated: true, completion: nil)


        } else if score == 200 {
            let ac = UIAlertController(title:"Great! You Win", message: "Play again or Wait for Next Challenge", preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "Ok", style: .Default, handler: askQuestion))
            presentViewController(ac, animated: true, completion: nil)
            scoreB.text = ""
            score = 0
            +score




        } else {
            let ac = UIAlertController(title: title, message: "Your score is \(score).", preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "Continue", style: .Default, handler: askQuestion))
            presentViewController(ac, animated: true, completion: nil)
            scoreB.text = "\(score)"
        }
    }

I can guess i have problem in if declaration.i am new to swift.so can any one to help me out.

Needed in if condition:

If answer is correct its should increase by showing uialert if answer is wrong it should decrease by showing uialert if score is <= the uialert message that show "you loss" if score is == 200 uialert message that shows"you win"

This only i have declared in my code .but some problem i am not able to figure out my problem !

Aucun commentaire:

Enregistrer un commentaire