gpt4 book ai didi

ios - 如何在 onReceive 计时器关闭 SwiftUI iOS 中导航另一个 View

转载 作者:行者123 更新时间:2023-12-01 16:11:29 25 4
gpt4 key购买 nike

当计时器达到特定时间时,我试图实现到另一个 View 的导航。例如,我想在 5 分钟后导航到另一个 View 。在 swift 中,我可以轻松实现这一点,但我是 SwiftUI 的新手,将不胜感激。
我的代码:

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

最佳答案

这是可能的方法(当然假设 TwitterWebView 在 parent 的某个地方有 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
}
}
}
}

关于ios - 如何在 onReceive 计时器关闭 SwiftUI iOS 中导航另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63238476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com