swift - 如何使用 SwiftUI 在警报中使用导航

标签 swift swiftui swift5.1 swiftui-navigationlink

我正在开发一个蓝牙应用程序。它有入职和仪表板。在入职上有配对和如何使用模块的说明,仪表板控制外围设备。所以我需要使用警报取消配对并将其导航到另一个名为 的页面入职 .如何使用警报导航到不同的 View 。

代码块

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")
    }

}

谢谢 !!!!

最佳答案

我为演示简化了您的代码快照,但认为这个想法很清楚

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
        }
    }
}

struct TestNavigationFromAlert_Previews: PreviewProvider {
    static var previews: some View {
        TestNavigationFromAlert()
    }
}

关于swift - 如何使用 SwiftUI 在警报中使用导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59276161/

相关文章:

swiftui - 为什么我的 SwiftUI 应用程序中没有更新 ObservedObject 数组?

ios - .newPassword for SecureField 选择强密码在使用 SwiftUI 和 iOS14 时不起作用

swift - 如何在 TextField 中将占位符文本居中

swift - 更改键盘动画弹跳的 Storyboard约束

ios - 我如何使用服务器中的数组并在服务中显示

ios - PresentationLink 始终使用相同的目标对象

ios - 更新到 Xcode 11 Swift 5.1 后出现 Google AdMob 错误

facebook - 快速登录facebook,无法获取值,

ios - 解析查询 .whereKey

SwiftUI:目标 View 的导航栏没有背景并且在滚动时没有动画