我正在尝试在收到一些数据后更新SwiftUI视图。我更新了actions: <Widget>[
PopupMenuButton<String>(
icon: Container(
child: ClipOval(
child: Align(
heightFactor: 1,
widthFactor: 1,
child: Image.network(googleUserUrl),
),
)
),
属性的值,但这没有任何作用。似乎该值实际上根本没有更新。我也不太幸运尝试使用@State
。
这是我正在尝试的游乐场示例
@Binding
此打印
import SwiftUI
struct SomeView: View {
// I've seen example of ObservableValue using classes, but as much as possible
// I'd like to use immutable structs for my state.
struct ViewModel {
let foo: [Int]
}
@State var viewModel: ViewModel
init(_ viewModel: ViewModel) {
self._viewModel = .init(initialValue: viewModel)
}
var body: some View {
Text(displayText)
}
var displayText: String {
print("displaying \(viewModel.foo.count) elements")
return "\(viewModel.foo.count)"
}
}
let view = SomeView(.init(foo: []))
print("will set new viewModel")
view.viewModel = .init(foo: [1, 2, 3]) // Looks like this doesn't do anything
print("did set new viewModel")
print("View now has \(view.viewModel.foo.count) elements")
我希望看到
displaying 0 elements
will set new viewModel
displaying 0 elements
did set new viewModel
View now has 0 elements