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

NavigationLink View in .toolbar(...): unwanted nested child views on model change

Forums > SwiftUI

I have two views:

  1. Main view with Navigation link to User's settings page
  2. User SettingsView, which allows to change user's settings (username)
struct User: Hashable {
    var id: Int
    var username: String
}

class Store: ObservableObject {
    @Published var user: User? = User(id: 1, username: "foo")

    func updateUser(to newUsername: String) {
        user?.username = newUsername
    }
}

@main
struct PlaygoundApp: App {
    @StateObject var store = Store()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(store)
        }
    }
}

struct ContentView: View {
    @EnvironmentObject var store: Store

    var body: some View {
        NavigationView {
            VStack{
                Text("Username: \(store.user?.username ?? "")")

                NavigationLink(
                    destination: SettingsView(user: store.user),
                    label: {
                        Text("Edit user")
                    })
            }

        }
    }
}

struct SettingsView: View {
    @EnvironmentObject var store: Store

    var user: User?

    var body: some View {
        Text(user?.username ?? "")
        Button(action: {
            store.updateUser(to: "New username")
        }, label: {
            Text("Update username")
        })
    }
}

Here's how it works:

But I put NavigationLink button to Toolbar, each model change creates a new navigational view:

struct HomeView: View {
    @EnvironmentObject var store: Store

    var body: some View {
        VStack {
            Text("Home View")
            Text("Username: \(store.user?.username ?? "")")
        }
    }
}

struct ContentView: View {
    @EnvironmentObject var store: Store

    var body: some View {
        NavigationView {
            HomeView()
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        if (store.user != nil) {
                            NavigationLink(
                                destination: SettingsView(user: store.user),
                                label: {
                                    Text("Edit user")
                                })
                        }
                    }
                }
        }
    }
}

Here's what happening:

How to aviod nested view creation?

2      

Hi f1nnix,

Think I found the problem,

Remove var user: User? from the SettingView and change Text(store.user?.username ?? "")

so SettingView should be this.

struct SettingsView: View {
    @EnvironmentObject var store: Store

//    var user: User? \\  <- remove

    var body: some View {
        VStack {
            Text(store.user?.username ?? "")
            Button(action: {
                store.updateUser(to: "New username")
            }, label: {
                Text("Update username")
            })
        }
        .navigationTitle("Setting")
    }
}

PS you need to remove user from call as well.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.