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

SOLVED: Project 7 Filter challenge - how to "refresh" navigation bar?

Forums > 100 Days of Swift

@beco0  

Hello, I was facing the challenge on the Project 7, the one with filtering petitions and i thought it'd be cool to put a "home" button on the navigation bar, that put all petitions in the table view after they were filtered. It's working fine if the button sits there all the time - I just add this to navigationItem.leftBarButtonItems array after view is loaded. Although, when all the petitions are already shown, there no point for this button to exist. It could be added to the leftBarButtonItems array during the app is running, but it doesn't appear there just like that - is there any method to reload navigation bar?

Here's my viewDidLoad method with the solution I tried (didn't work):

override func viewDidLoad() {
        super.viewDidLoad()
        let urlString: String

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(showCredits))
        let home = UIBarButtonItem(title: "Home", style: .plain, target: self, action: #selector(loadHomeView))
        let filter = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(filterPetitions))
        navigationItem.leftBarButtonItems = [filter]

        if navigationController?.tabBarItem.tag == 0 {
            urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"
        } else {
            urlString = "https://www.hackingwithswift.com/samples/petitions-2.json"
        }

        if let url = URL(string: urlString) {
            if let data = try? Data(contentsOf: url) {
                parse(json: data)
                return
            }
        }
        showError()

        if petitions.count != originalPetitions.count {
            navigationItem.leftBarButtonItems?.insert(home, at: 0)
        }
    }

There are two arrays of petitions, since I decided to store the data from JSON as the "originalPetitions" and "petitions" as the array to show in tableview. The "loadHomeView" method assign originalPetitions to petitions and reload tableview, the "filterPetitions" assign filtered petitions to petitions array - it's working fine.

3      

I did something similar, but I replaced the filter button with a button to clear the filter whenever the filter was active. Then in the code where the filter gets cleared, I just set it back.

So i my viewDidLoad I initialized two view controller properties and then set the navigation bar button to the filter icon like so:

filterButton = UIBarButtonItem(image: UIImage(systemName: "line.horizontal.3.decrease.circle"), style: .plain, target: self, action: #selector(filterPetitions))
clearFilterButton = UIBarButtonItem(image: UIImage(systemName: "xmark"), style: .plain, target: self, action: #selector(clearFilter))

navigationItem.leftBarButtonItem = filterButton

Then in my filterPetitions method, I set navigationItem.leftBarButtonItem = clearFilterButton and in clearFilter I set it back to filterButton.

4      

@beco0  

Thanks for your respond! It works great - in my case I change the navigationBar.leftBarButtonItems array from [filter] to [home, filter] and backwards :D

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.