UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 22 - Challenge 3 (linked to project 2)

Forums > 100 Days of Swift

Hi everyone,

I am stuck on the last part of this challenge: "Go back to project 2 and add a bar button item that shows their score when tapped."

I was able to create a leftBarButtonItem to display the score but it won't update when the actual score is changed - it stays at its initial value of 0. Do you have an idea how to make it update?

Here is what I have done so far:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Score \(getScore())", style: .plain, target: self, action: #selector(getScore))

and:

@objc func getScore() -> Int {
    return score
}

Here I have made a getScore function because using score directly or through another property did not work. But that is not the solution either.

I would appreciate any help, so thanks in advance!

3      

Though your effort is admirable, I think that you went quite too far here :)

Your task here is to show the score simply by a UIVIewController, whether it would be an Alert or Activity one. Since the whole topic of Project 3 was the UIActivityViewController, I used that to show the score (with additional possibility of sharing it wherever I want).

So you don't have to worry about creating additional functions and stuff. Just use UIActivityViewController, where you will show your score using 'score' property, like this:

"Score: \(score)"

Let me know if that helps!

3      

Hello MateusZ,

Thanks for your response.

I was indeed in the wrong path! I have now solved the issue with a UIActivityViewController as you suggested.

Thank you for your help!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.