简体   繁体   English

为什么这个 swiftui 视图没有更新?

[英]Why does this swiftui view not update?

Consider the following code block: import SwiftUI考虑以下代码块:import SwiftUI

struct MeasurementReading: View, Equatable {

    @ObservedObject var ble: BluetoothConnectionmanager
    @GestureState var isDetectTap = false
    @State var MyText:String = "Wait"

    static func == (lhs: MeasurementReading, rhs: MeasurementReading)->Bool{
        return lhs.MyText == rhs.MyText
    }

    var body: some View {
        let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

        return HStack {
            Spacer()
            VStack{
                Button(action:{
                    self.MyText = "\(self.ble.getValue()!) mV"
                    print("Text is \(self.MyText as NSString)")
                }, label: {
                    Text(MyText)
                        .font(.system(size: 40))
                        .bold()
                        .foregroundColor(Color.black)
                        .padding(.trailing, 15)
                        .frame(height: 100)
                })
                Button(action: {
                    self.MyText = "\(self.ble.getValue()!) mV"
                    print("Text is \(self.MyText as NSString)")

                }, label: {
                    Text(MyText)
                        .font(.system(size: 25))
                        .padding(.top, -20)
                        .padding(.bottom, 20)
                        .foregroundColor(Color.black)
                })
            }
        }.onReceive(timer)
        { _ in // TIMER FUNCTIONALITY HERE
            self.MyText = "\(self.ble.getValue()!) mV"
            print("Text is \(self.MyText)")
        }
    }
}


struct MeasurementReading_Previews: PreviewProvider {
    static var previews: some View {
        MeasurementReading(ble: BluetoothConnectionmanager())
    }
}

Every 1 second the correct value read from the BLE system is assigned to MyText and then MyText is printed to the debug output properly with the updated value.每隔 1 秒,将从 BLE 系统读取的正确值分配给 MyText,然后 MyText 将使用更新的值正确打印到调试输出。

The problem here is that view MeasurementReading does not update.这里的问题是视图 MeasurementReading 不会更新。 Also, using a closure on any item also has the same behavior (variable is updated, it is output properly but no view update) ex .onTap{....} will have the same behavior or any other .onXXXX closure.此外,在任何项目上使用闭包也具有相同的行为(变量被更新,它被正确输出但没有视图更新)例如 .onTap{....} 将具有相同的行为或任何其他 .onXXXX 闭包。 The only way I could get the view to update at all with new values for the MyText state is to put the behavior in a Button.我可以使用 MyText 状态的新值更新视图的唯一方法是将行为放在一个按钮中。

My question is this: Why does the view not update even when the state variable changes via Timer or .onXXXX closure?我的问题是:为什么即使状态变量通过 Timer 或 .onXXXX 闭包更改,视图也不会更新?

You need to be setting the ble value to the updated timer value:您需要将ble值设置为更新的计时器值:

Without testing this properly.如果没有正确测试。 I also think your BluetoothConnectionmanager needs to be a @State property for this to work.我还认为您的BluetoothConnectionmanager需要是一个@State属性才能工作。

@State var ble: BluetoothConnectionmanager

.onReceive(timer) { value in // value is the updated value
    self.ble.value = value
    self.MyText = "\(self.ble.getValue()!) mV"
    print("Text is \(self.MyText)")
}

Take a look at this example to see how a timer works with a Date() object.查看示例以了解计时器如何与Date()对象一起使用。

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

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