简体   繁体   English

如何在 onReceive 计时器关闭中导航另一个视图 SwiftUI iOS

[英]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.例如,我想在 5 分钟后导航到另一个视图。 In swift i can easily achieve this but I am new to SwiftUI and little help will be highly appreciated.在 swift 中,我可以轻松实现这一点,但我是 SwiftUI 的新手,我们将不胜感激。

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)这是可能的方法(当然假设TwitterWebView在父母的某处有NavigationView

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 SwiftUI 中没有 NavigationButton 的情况下导航到 NavigationView 中的另一个视图? - How to navigate to another view in NavigationView without NavigationButton in SwiftUI? SwiftUI:如何从 UIViewRepresentable 导航到 SwiftUI 视图 - SwiftUI: How to navigate from UIViewRepresentable to SwiftUI View 如何在ios中从一个视图导航到另一个视图? - How to Navigate from one view to another view in ios? 当环境对象更新导致同一视图重新加载时,如何导航到另一个 SwiftUI 视图 - How do I navigate to another SwiftUI View when an Environment Object update is causing the same view to reload 如何从 SwiftUI 视图导航回 UIViewController? - How to navigate back to UIViewController from SwiftUI View? 如何在没有 NavigationLink 的情况下导航到 SwiftUI 中的视图 - How to navigate to a View in SwiftUI without NavigationLink 导航到 SwiftUI 中的相同视图 - Navigate to same view in SwiftUI iOS:无法导航到其他视图控制器 - iOS: Not able to navigate to another view controller ios在另一个视图中的tableview中导航和显示数据 - ios navigate and display data in tableview on another view SwiftUI 如何在子视图上导航到新的导航页面 - SwiftUI How do I navigate on Child View to a new navigation page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM