Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

ios - 为什么这个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状态的新值来更新视图的唯一方法是将行为放在Button中。)

My question is this: Why does the view not update even when the state variable changes via Timer or .onXXXX closure?

(我的问题是:即使状态变量通过Timer或.onXXXX闭包更改,为什么视图也不更新?)

  ask by Matt translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...