计时器更新视图时SwiftUI导航栏按钮不工作
当计时器更新视图时,似乎触发导航栏按钮有问题。 在下面的示例中,“关闭”按钮起作用,但“导航栏”按钮“取消”不起作用。 知道为什么吗?或者如何修复计时器更新视图时SwiftUI导航栏按钮不工作,swiftui,Swiftui,当计时器更新视图时,似乎触发导航栏按钮有问题。 在下面的示例中,“关闭”按钮起作用,但“导航栏”按钮“取消”不起作用。 知道为什么吗?或者如何修复 struct ContentView: View { @State var isPresented = false var body: some View { Button(action: { isPresented = true }, label: {
struct ContentView: View {
@State var isPresented = false
var body: some View {
Button(action: {
isPresented = true
}, label: {
Text("Button")
})
.sheet(isPresented: $isPresented, content: {
ModalView()
})
}
}
class Model: ObservableObject {
private var monitorTimer: Timer?
@Published var someThing: Bool = false
init() {
startMonitoring()
}
func startMonitoring() {
monitorTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { _ in
self.someThing.toggle()
})
}
}
struct ModalView: View {
@Environment(\.presentationMode) private var presentationMode
@StateObject var model = Model()
var body: some View {
NavigationView {
VStack{
if model.someThing {
Text("True")
} else {
Text("False")
}
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Close")
})
}
.navigationBarTitle("Modal")
.navigationBarItems(
leading:
Button("Cancel") {
presentationMode.wrappedValue.dismiss()
})
}
}
}
这里是经过测试的解决方法(Xcode 12/iOS 14)。解决方案是分离子视图以避免全局刷新,如下所示
注意:顺便说一句-不要忘记在关闭模式下停止计时器,因为状态对象保持活动状态,所以现在即使在工作表关闭后计时器也会运行
struct ModalView: View {
@StateObject var model = Model()
var body: some View {
InternalModal().environmentObject(model)
}
}
struct InternalModal: View {
@Environment(\.presentationMode) private var presentationMode
var body: some View {
NavigationView {
VStack{
UpdatingView()
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Close")
})
}
.navigationBarTitle("Modal")
.navigationBarItems(
leading:
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Cancel")
})
}
}
}
struct UpdatingView: View {
@EnvironmentObject var model: Model
var body: some View {
if model.someThing {
Text("True")
} else {
Text("False")
}
}
}
这里是经过测试的解决方法(Xcode 12/iOS 14)。解决方案是分离子视图以避免全局刷新,如下所示
注意:顺便说一句-不要忘记在关闭模式下停止计时器,因为状态对象保持活动状态,所以现在即使在工作表关闭后计时器也会运行
struct ModalView: View {
@StateObject var model = Model()
var body: some View {
InternalModal().environmentObject(model)
}
}
struct InternalModal: View {
@Environment(\.presentationMode) private var presentationMode
var body: some View {
NavigationView {
VStack{
UpdatingView()
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Close")
})
}
.navigationBarTitle("Modal")
.navigationBarItems(
leading:
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Text("Cancel")
})
}
}
}
struct UpdatingView: View {
@EnvironmentObject var model: Model
var body: some View {
if model.someThing {
Text("True")
} else {
Text("False")
}
}
}
这个bug更具全局性-即使您删除了if/else块,取消也不起作用。在调试器中,我并没有进入断点上的按钮操作块,所以它在SwiftUI中很深。事实并非如此,因为从视图层次结构调试中可以看出,矩形现在布局正确。我建议向苹果提交反馈。使用Xcode 12/iOS 14进行测试。该错误更具全局性-即使您删除了if/else块,取消也不起作用。在调试器中,我并没有进入断点上的按钮操作块,所以它在SwiftUI中很深。事实并非如此,因为从视图层次结构调试中可以看出,矩形现在布局正确。我建议向苹果提交反馈。使用Xcode 12/iOS 14进行测试。