3

如何在没有任何交互的情况下在 swiftUI 中 x 秒后推送视图?

 NavigationView{
            .onAppear {
                DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
                    NavigationLink(destination: pageViewUi()) {
                        EmptyView()
                    }
                }
            }
}
4

2 回答 2

8

你可以这样处理它:


struct ContentView: View {
    @State var show = false
    var body: some View {
        NavigationView{
            NavigationLink(destination: Text("OK"), isActive: $show, label: {
                EmptyView()
            })
                .onAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                        self.show.toggle()
                    }
            }
        }
    }
}

于 2020-02-14T09:14:03.203 回答
0

您需要使用状态变量来管理它:

import SwiftUI

struct Home : View {
    @State var isActive = false

    var body: some View {
        NavigationView {
            VStack {
                Text("Home")
                NavigationLink(destination: Details(), isActive: self.$isActive) {
                    EmptyView()
                }
            }
        }.onAppear {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                self.isActive.toggle()
            }
        }
    }
}

struct Details : View {
    var body: some View {
        VStack {
            Text("sample code")
        }.navigationBarBackButtonHidden(true) // To hide back button
    }
}
于 2020-02-14T09:19:08.793 回答