'init(_:isActive:destination:)' 在 iOS 16.0 中被弃用:在 NavigationStack 或 NavigationSplitView 中使用 Naviga



我尝试通过LBTA构建一个应用程序,作者使用的是NavigationLink方法:

var body: some View {
NavigationView {
VStack{

customNavBar
messagesView

NavigationLink("", isActive: $shouldNavigateToChatLogView){
Text("Chat Log View")
}

}
.overlay(newMessageButton, alignment: .bottom)

}.navigationBarHidden(true)


}

但是这个方法在IOS 16中被弃用了,我使用它,我正试图通过新的方法来实现它NavigationLink(_:value:),但我不明白如何正确地做代码工作。我是这样做的:

var body: some View {
NavigationStack {
VStack{

customNavBar
messagesView

NavigationLink(value: shouldNavigateToChatLogView){
Text("Chat Log View")
}

}
.overlay(newMessageButton, alignment: .bottom)

}.navigationBarHidden(true)


}

代码已编译,但NavigationLink不再工作。

我曾经在苹果的SwuftUI迁移网站和stackowerflow中搜索过这个,但我只是不明白它是如何工作的。由于

您缺少.navigationDestination:

var body: some View {
NavigationStack {
VStack{

customNavBar
messagesView

NavigationLink(value: shouldNavigateToChatLogView){
Text("Chat Log View")
}
.navigationDestination(for: ChatLog.self) { log in
ChatLogView(log: log)
}
}
.overlay(newMessageButton, alignment: .bottom)

}.navigationBarHidden(true)
}

假设你有一个ChatLogView取一个ChatLog

最新更新