简体   繁体   中英

using navigation links xcode swift IOS 16

Hello I am trying to use a simple navigation link but it seems like this method has been depreciated with this new IOS update. I tried looking at the apple doc to try and fix it but im not sure how. I get the following error "depreciated in IOS 16: use NavigationLink(value:label:) inside a navigationStack or NavigationSplitView" I will also add my code below. Anyone know how to format it correctly?

@EnvirnmentObject var viewModel: AuthViewModel()

var body: some View {
        NavigationLink(destination: ProfileView(), isActive: 
                       $viewModel.user, label: {})
    }
}

in iOS 16 Navigation was significantly changed. If your Navigation looks like:

@State private var viewModel = ViewModel()

var body: some View {
    NavigationView { 
        List {
            NavigationLink("User name", isActive: $viewModel.user) {
                UserDetail(user: viewModel.user)
            }
        }
    }
    .navigationViewStyle(.stack) 
}

use instead


@State private var viewModel = ViewModel()

var body: some View {
    NavigationStack {
        List {
            NavigationLink("User Name", value: user)
        }
        .navigationDestination(for: User.self) { user in
            UserDetail(user: user)
        }
    }
}

You can find more detail in https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM