1

I am creating a Quiz app where each quiz question is a grouped TableView with each cell being an answer choice, and is embedded in a navigation controller. For each right answer a user taps, I want their score to go up by 1. I have set up a score label as a rightBarButtonItem in my navigation controller.

Here is what I have for creating the bar button item in viewDidLoad( ):

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Score: \(score)", style: .plain, target: nil, action: Selector(("updateScore")))

I have a model class Question that contains an array questionsList which includes the properties: questionString, answers[], selectedAnswerIndex (nil), and correctAnswerIndex (int)

The updateScore method:

@objc func updateScore() {

        for question in questionsList {

            if question.selectedAnswerIndex == question.correctAnswerIndex {
                score += 1
            }
        }
    }

Any thoughts? I tried doing this another way by putting the score label in a footerview, using viewForFooterInSection for the table controller, and also putting the for loop in my didSelectRowAt method but the score label wouldn't update there either.

1 Answer 1

1

After you update score, you need to create and assign a new bar button item. You can't update the text of the existing button.

After your for loop, add:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Score: \(score)", style: .plain, target: nil, action: Selector(("updateScore")))

Yes, it's the same code as what you show for creating the original button.

A better approach would be to update your score property:

var score: Int = 0 {
    didSet {
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Score: \(score)", style: .plain, target: nil, action: Selector(("updateScore")))
    }
}

Then update your updateScore:

@objc func updateScore() {
    var newScore = score
    for question in questionsList {
        if question.selectedAnswerIndex == question.correctAnswerIndex {
            newScore += 1
        }
    }

    score = newScore
}

Then update viewDidLoad (or wherever) and remove the current call to create the bar button item and simply do:

score = 0 // or some other appropriate initial value
5
  • Thanks for the response but where would I put the code to create and assign the new bar button item? Right after where I created the first bar button item?
    – user11601508
    Aug 16, 2019 at 0:45
  • Ok. I'm confused on what I am supposed to title the new bar button item and put in for the action. Doesn't make much sense to me to name it score and give the updateScore method as the selector since thats already in the viewDidLoad.
    – user11601508
    Aug 16, 2019 at 1:44
  • Thanks. After I added the didSet part, it was giving me a "class 'QuestionTableViewController' has no initializers" error, so I added init(userScore: int) and the required init for the tableviewcontroller, but now I am getting a "cannot invoke initializer for type 'QuestionTableViewController' with no arguments" error on my let questionTableViewController = QuestionTableViewController( ) in my didSelectRowAt method (pushes the next question based on if user selects a row)..
    – user11601508
    Aug 16, 2019 at 2:22
  • Just initialize score to 0. I updated the answer.
    – rmaddy
    Aug 16, 2019 at 2:24
  • Thanks for the help but score label doesn't seem to be updating.. I don't think its my Question model class that contains the questionsList array as I have gotten my scoring to work before (not on a bar button item) with the same for loop... should I look into other ways to represent the score as the user goes through the quiz?
    – user11601508
    Aug 16, 2019 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.