SwiftUI – NavigationLink 创建新项目并导航到新视图

huangapple go评论78阅读模式
英文:

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 Flights 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)

huangapple
  • 本文由 发表于 2023年7月24日 17:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定