如何在SwiftUI中使用警报中的导航?

5
我正在开发一个蓝牙应用程序,它有上线和仪表板。在上线过程中,有配对和使用模块的说明,而仪表板可以控制外围设备。因此,我需要使用警报来解除配对,并将其导航到名为Onboarding的不同页面。如何使用警报导航到不同视图。

代码块

import SwiftUI
import BLE

struct Dashboard: View {

    @EnvironmentObject var BLE: BLE
    @State private var showUnpairAlert: Bool = false
    @State private var hasConnected: Bool = false

    let defaults = UserDefaults.standard
    let defaultDeviceinformation = "01FFFFFFFFFF"

    struct Keys {
        static let deviceInformation = "deviceInformation"
    }

    var body: some View {
        VStack(alignment: .center, spacing: 0) {
            // MARK: - Menu Bar
            HStack(alignment: .center, spacing: 10) {
                VStack(alignment: .center, spacing: 4) {
                    Text(self.hasConnected ? "PodId \(checkForDeviceInformation())":"Pod is not connected")
                        .font(.footnote)
                        .foregroundColor(.white)
                    Button(action: {
                        print("Unpair tapped!")
                        self.showUnpairAlert = true
                    }) {
                        HStack {
                            Text("Unpair")
                                .fontWeight(.bold)
                                .font(.body)
                        }
                        .frame(minWidth: 85, minHeight: 35)
                        .foregroundColor(.white)
                        .background(Color(red: 0.8784313725490196, green: 0.34509803921568627, blue: 0.36470588235294116))
                        .cornerRadius(30)
                    }
                }
            }
        }
        .alert(isPresented: $showUnpairAlert) {
            Alert(title: Text("Unpair from \(checkForDeviceInformation())"), message: Text("Do you want to unpair the current pod?"), primaryButton: .destructive(Text("Unpair")) {
                self.unpairAndSetDefaultDeviceInformation()
                }, secondaryButton: .cancel())
        }
    }

    func checkForDeviceInformation() -> String {
        let deviceInformation = defaults.value(forKey: Keys.deviceInformation) as? String ?? ""
        print("Device Info \(deviceInformation)")
        return deviceInformation
    }

    func unpairAndSetDefaultDeviceInformation() {
        defaults.set(defaultDeviceinformation, forKey: Keys.deviceInformation)
        print("Pod unpaired and view changed to Onboarding")
    }

}

非常感谢!!!


你看过这个吗:https://www.hackingwithswift.com/books/ios-swiftui/using-an-alert-to-pop-a-navigationlink-programmatically? - sugar2code
是的@sugar2code,我尝试使用演示模式,但那只是为了关闭视图。我想要导航到不同的视图。 - Denzil
1
哦,我明白了!我认为这篇文章很相似:https://dev59.com/DLXna4cB1Zd3GeqPSuyV。他们有一个警报来检查错误,然后如果一切顺利就导航。所以你需要调整按钮操作,但我认为原则是相同的,即在你的主视图中有一个 navigationlink,在某些环境变量为 true 的情况下可见,然后通过警报按钮切换该状态。希望这可以帮助到你! - sugar2code
1个回答

9

我为演示简化了您的代码快照,但是想法应该很清晰

struct TestNavigationFromAlert: View {

    @State private var showUnpairAlert: Bool = false
    @State private var activateLink: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: Text("Your Onboarding page"), isActive: $activateLink,
                    label: { EmptyView() })

                // DEMO BUTTON - REMOVE IT
                Button(action: { self.showUnpairAlert = true }) { Text("Alert") }

                // YOUR CODE IS HERE
            }
            .alert(isPresented: $showUnpairAlert) {
                 Alert(title: Text("Unpair from \(checkForDeviceInformation())"), message: Text("Do you want to unpair the current pod?"), primaryButton: .destructive(Text("Unpair")) {
                     self.unpairAndSetDefaultDeviceInformation()
                     }, secondaryButton: .cancel())
             }
        }
    }

    func checkForDeviceInformation() -> String {
        // YOUR CODE IS HERE
        return "Stub information"
    }

    func unpairAndSetDefaultDeviceInformation() {
        // YOUR CODE IS HERE
        DispatchQueue.main.async {
            self.activateLink = true
        }
    }
}

已经能用,但是已经被弃用了。 - abbasalim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接