SwiftUI:如何弹出WatchOS的根视图?

时间:2020-06-05 03:35:23

标签: swiftui watchkit apple-watch watchos

我需要从详细视图弹出到根视图。尽管以下使用isDetailList和isActive的解决方案在iOS上运行良好,但在watchOS上却不起作用。在watchOS中,isDetailList命令不可用。

import SwiftUI

class AppState : ObservableObject {
    @Published var showState : Bool = false
}

struct MoreTests: View {
    @EnvironmentObject var appState : AppState    // injected from SceneDelegate
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: MoreView1(),
                isActive: $appState.showState,      // required to work
                label: { Text("Go to MoreView1") }
            ).isDetailLink(false)       // required to work
        }.navigationBarTitle("Root")
    }
}

struct MoreView1: View {
    var body: some View {
        NavigationLink(
            destination: MoreView2(),
            label: { Text("Go to MoreView2") }
        )
        .navigationBarTitle("MoreView1")
    }
}

struct MoreView2: View {
    var body: some View {
        NavigationLink(
            destination: MoreView3(),
            label: { Text("Go to MoreView3") }
        )
        .navigationBarTitle("MoreView2")
    }
}

struct MoreView3: View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var appState : AppState
    var body: some View {
        VStack {
            Button(action: {
                self.appState.showState = false       // required
            }) {
                Text("Dismiss to root")
            }
        }.navigationBarTitle("MoreView3")
    }
}

iOS解决方案来自这里-> SwiftUI: How to pop to Root view

0 个答案:

没有答案