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

View does not refresh

Forums > SwiftUI

@NM97  

In the view whose code you see below I have a button that calls the searchView function. It displays an alert and in it we enter the number by which we are looking for an object and this object is returned by the getSong function. Up to this point everything is working fine. The problem I have encountered is that it now calls view and passes this found object, but the view does not refresh. I must have made some mistake that I can't locate or that I have forgotten. I would appreciate some help with this.

struct DetailView: View {
    @State var song : Song
    @State var isSelected: Bool
    var body: some View {
        VStack{
            Text(song.content ?? "No content")
                .padding()
            Spacer()
        }
        .navigationBarTitle("\(song.number). \(song.title ?? "No title")", displayMode: .inline)
        .toolbar {
            ToolbarItemGroup(placement: .navigationBarTrailing) {
                HStack{
                    Button(action: {
                        song.favorite.toggle()
                        isSelected=song.favorite
                    }) {
                        Image(systemName: "heart.fill")
                            .foregroundColor(isSelected ? .red : .blue)
                    }

                    Button(action: {
                        searchView()
                    }) {
                        Image(systemName: "magnifyingglass")
                    }
                }
            }
        }
    }
    func searchView(){

        let search = UIAlertController(title: "Go to the song", message: "Enter the number of the song you want to jump to.", preferredStyle: .alert)
        search.addTextField {(number) in
            number.placeholder = "Song number"
        }

        let go = UIAlertAction(title: "Go", style: .default) { (_) in
            let songFound = PersistenceController.shared.getSong(number: (search.textFields![0].text)!)
            DetailView(song: songFound!, isSelected: songFound!.favorite)
        }
        let cancel = UIAlertAction(title: "Cancel", style: .destructive) { (_) in
            print("Cancel")
        }
        search.addAction(cancel)
        search.addAction(go)

        UIApplication.shared.windows.first?.rootViewController?.present(search, animated: true, completion: {

        })
    }
}

2      

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.