简体   繁体   中英

How to navigate another view in onReceive timer closure SwiftUI iOS

I am trying to achieve a navigation to another view when timer hits a specific time. For example I want to navigate to another view after 5 minutes. In swift i can easily achieve this but I am new to SwiftUI and little help will be highly appreciated.

My code:

struct TwitterWebView: View {
    
    @State var timerTime : Float
    @State var minute: Float = 0.0
    @State private var showLinkTarget = false
    let timer = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()
    
    var body: some View {
        
        WebView(url: "https://twitter.com/")
        .navigationBarTitle("")
        .navigationBarHidden(true)
        
        .onReceive(timer) { _ in
            if self.minute == self.timerTime {
                print("Timer completed navigate to Break view")
                NavigationLink(destination: BreakView()) {
                    Text("Test")
                }
                self.timer.upstream.connect().cancel()
            } else {
                self.minute += 1.0
            }
        }
    }
}

Here is possible approach (of course assuming that TwitterWebView has NavigationView somewhere in parents)

struct TwitterWebView: View {

    @State var timerTime : Float
    @State var minute: Float = 0.0
    @State private var showLinkTarget = false
    let timer = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()

    @State private var shouldNavigate = false

    var body: some View {

        WebView(url: "https://twitter.com/")
        .navigationBarTitle("")
        .navigationBarHidden(true)

        .background(
            NavigationLink(destination: BreakView(), 
                              isActive: $shouldNavigate) { EmptyView() }
        )

        .onReceive(timer) { _ in
            if self.minute == self.timerTime {
                print("Timer completed navigate to Break view")
                self.timer.upstream.connect().cancel()

                self.shouldNavigate = true      // << here
            } else {
                self.minute += 1.0
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM