如何使用SwiftUI在警报中使用导航 [英] How can I use Navigation in alert using SwiftUI

查看:106
本文介绍了如何使用SwiftUI在警报中使用导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用蓝牙应用程序,它具有入门和仪表板.入门中有配对和有关如何使用该模块的说明,并且仪表板控制外围设备,因此我需要使用警报和导航到另一个名为 Onboarding 的页面.如何使用警报导航到另一个视图.

I'm working on a Bluetooth Application.It has onboarding and dashboard.On the Onboarding there is pairing and instructions on how to use the module, and the dashboard controls the peripheral device.So I need to unpair using an alert and navigate it to a different page called Onboarding.How can I navigate to a different view using an alert.

代码块

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

}

谢谢!!!!

推荐答案

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

I simplified your code snapshot for demo, but think the idea would be clear

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

这篇关于如何使用SwiftUI在警报中使用导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆