SwiftUI iOS 17 Xcode 15 将带有操作的按钮更改为 NavigationLink 或?

SwiftUI iOS 17 Xcode 15 change a button with an action to a NavigationLink or?

提问人:Mike Haslam 提问时间:11/15/2023 最后编辑:Mike Haslam 更新时间:11/16/2023 访问量:55

问:

我有一个视图,它是在布尔值的状态上有条件地呈现的。

它目前有效,但我正在尝试清除此警告:

“init(destination:isActive:label:)”在 iOS 16.0 中已弃用:在 NavigationStack 或 NavigationSplitView 中使用 NavigationLink(value:label:) 或 navigationDestination(isPresented:destination:)

因此,当按下按钮中包装的视图时,我尝试导航到视图。


import SwiftUI

struct Question: View {
    @EnvironmentObject var invoiceViewModel: InvoiceViewModel
    @EnvironmentObject var settingsViewModel: SettingViewModel
    @State private var showingAlert = false
    
    var body: some View {
        NavigationStack{
            ZStack {
                // THIS WORK BUT IS DEPRECIATED
                NavigationLink(destination: BusinessDetails(), isActive: $invoiceViewModel.isShowBusinessDetaisLinkActive) {
                    EmptyView()
                }
                if invoiceViewModel.invoices().count == 0 {
                    // do stuff
                }else{
                    List{
                        // show created items
                    }
                    // THIS WORKS FOR OTHER VIEWS BUT NOT FOR BusinessDetails()
                    .navigationDestination(isPresented: $invoiceViewModel.isShowInvoiceDetaisLinkActive) {
                        InvoiceDetailView()
                    }
                }
                
                
                VStack{
                    //THIS IS WHERE CONDITIONAL RENDERING IS TOGGLING THE BOOL
                    // THAT FIRES THE NAVIGATION LINK WHERE I AM GETTING DEPRECAITION WARNING
                    if !settingsViewModel.checkBusinessDetailsComplete() {
                        Button(action: {
                            invoiceViewModel.isShowBusinessDetaisLinkActive.toggle()
                        }, label: {
                            
                            BusinessDetailsHeader()
                                .clipShape(RoundedRectangle(cornerRadius: 10))
                            
                        })
                        
                        .padding()
                    }
                    Spacer()
                }
                
            }
        .navigationDestination(isPresented: $invoiceViewModel.isShowAddNewInvoice) {
          AddNewInvoice()
       }
            .navigationTitle("Recent Invoices")
            .toolbar(content: {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button{
                        if !settingsViewModel.checkBusinessDetailsComplete() {
                            showingAlert = true
                        }else{
                            withAnimation{
                                invoiceViewModel.showAddNewInvoice()
                            }
                        }
                    } label: {
                        Label("Add", systemImage: "plus.circle")
                    }
                    
                    .alert("Complete Your Business Details", isPresented: $showingAlert) {
                        Button("Business Details") {
                            invoiceViewModel.isShowBusinessDetaisLinkActive.toggle()
                        }
                    }
                }
            })
            
        }
    }
}

#Preview {
    Question()
}
iOS 的 SwiftUI XCode15

评论

0赞 Sweeper 11/16/2023
您已经知道如何使用 .为什么不用它来呢?.navigationDestination(isPresented: ...)isShowBusinessDetaisLinkActive
0赞 Mike Haslam 11/16/2023
事实证明,这是我试图调用 navigationDestination 的地方。当我在 NavigationStack 末尾调用所有 navigationDestination 调用时,它可以工作

答:

0赞 Mike Haslam 11/16/2023 #1

它只是我调用我的多个 navigationDestination 调用的地方。只需要在 NavigationStack 结束时调用它们