英文:
SwiftUI - NavigationLink creating new item and navigating to the new view
问题
如何修改我的代码,以便当用户点击"plus.app.fill"按钮时,他们可以直接进入新创建的航班的子视图?以下是我目前正在使用的代码:
struct FlightsOverView: View {
@ObservedObject var viewModel : FlightVM
var body: some View {
NavigationStack{
List {
ForEach(viewModel.flights, id: \.id) { flight in
ZStack {
NavigationLink(destination: FlightDetailView(flight: flight, viewModel: viewModel), label: {
FlightCell(flight: flight)
.listRowInsets(EdgeInsets(top: 5, leading: 8, bottom: 5, trailing: 8))
}).opacity(0)
FlightCell(flight: flight)
.listRowInsets(EdgeInsets(top: 5, leading: 8, bottom: 5, trailing: 8))
}
}
.onDelete(perform: viewModel.removeFlight)
}
.listStyle(PlainListStyle())
.navigationTitle("Flights")
.toolbar(content: {
Button("\(Image(systemName: "plus.app.fill"))", action: {
let flight = viewModel.createNewFlight()
viewModel.addFlight(flight: flight)
// Add code here to navigate to the subview of the newly created flight
})
})
}
}
}
目前,当点击"plus.app.fill"时,它会创建一个新的空航班,但我也想导航到新的子视图。我尝试了"isActive"和其他建议,但这些在iOS 16.0中已弃用。"init(destination:isActive:label:)"在iOS 16.0中已弃用:请在NavigationStack或NavigationSplitView中使用NavigationLink(value:label:)。
英文:
How can I modify my code so that when the user taps the "plus.app.fill" button, they are directly taken to the subview of the newly created flight? Below is the code that I'm currently using:
struct FlightsOverView: View {
@ObservedObject var viewModel : FlightVM
var body: some View {
NavigationStack{
List {
ForEach(viewModel.flights, id: \.id) { flight in
ZStack {
NavigationLink(destination: FlightDetailView(flight: flight, viewModel: viewModel), label: {
FlightCell(flight: flight)
.listRowInsets(EdgeInsets(top: 5, leading: 8, bottom: 5, trailing: 8))
}).opacity(0)
FlightCell(flight: flight)
.listRowInsets(EdgeInsets(top: 5, leading: 8, bottom: 5, trailing: 8))
}
}
.onDelete(perform: viewModel.removeFlight)
}
.listStyle(PlainListStyle())
.navigationTitle("Flights")
.toolbar(content: {
Button("\(Image(systemName: "plus.app.fill"))", action: {
let flight = viewModel.createNewFlight()
viewModel.addFlight(flight: flight)
})
})
}
}
}
At the moment when the "plus.app.fill" is tapped it creates a new empty flight but I would also like to navigate to the new subview. I tried "isActive" and other suggestions here but these were deprecated in iOS 16.0 "'init(destination:isActive:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a NavigationStack or NavigationSplitView"
答案1
得分: 2
将一个名为Flight
的数组绑定到NavigationStack
作为其导航路径。然后,您只需将新创建的Flight
添加到路径中以进行导航。
假设Flight
符合Hashable
协议
@State var navigationPath: [Flight] = []
NavigationStack(path: $navigationPath) {
List {
...
}
.navigationDestination(for: Flight.self) { flight in
FlightDetailView(flight: flight, viewModel: viewModel)
}
}
在ForEach
中,将每个航班传递给NavigationLink
:
NavigationLink(value: flight) {
// 在此处构建标签...
}
当添加新的航班时,
let flight = viewModel.createNewFlight()
viewModel.addFlight(flight: flight)
navigationPath.append(flight)
英文:
Bind an array of Flight
s to the NavigationStack
as its navigation path. Then you can just add the newly created Flight
to the path to navigate there.
Assuming Flight
conforms to Hashable
@State var navigationPath: [Flight] = []
NavigationStack(path: $navigationPath) {
List {
...
}
.navigationDestination(for: Flight.self) { flight in
FlightDetailView(flight: flight, viewModel: viewModel)
}
}
In the ForEach
, pass each flight to the NavigationLink
:
NavigationLink(value: flight) {
// build the label here...
}
When adding a new flight,
let flight = viewModel.createNewFlight()
viewModel.addFlight(flight: flight)
navigationPath.append(flight)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论