SwiftUI - 反转布尔绑定 [英] SwiftUI - Invert a boolean binding
本文介绍了SwiftUI - 反转布尔绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 swiftUI 视图中有一个绑定.类似的东西:
I have a binding in a swiftUI view. Something along the lines of :
struct MyCoolView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
Text("Here is a cool Text!").sheet(isPresented: $viewModel.MyProperty) {
SomeModalView()}
}
}
我希望 isPresented 使用存储在属性中的内容的 相反 布尔值.
I want the isPresented to use the opposite boolean value of what is stored in the property.
Swift 不会让我做类似的事情
Swift wont let me just do something like
.sheet(isPresented: !$viewModel.MyProperty)
(它给了我一个关于无法将 Binding
转换为 Bool
的错误)
(It gives me an error about cannot convert Binding <Bool>
to Bool
)
有没有关于如何处理这个问题的想法?
Any thoughts on how to deal with this?
推荐答案
您可以自行构建绑定:
Text("Here is a cool Text!").sheet(isPresented:
Binding<Bool>(get: {return !self.viewModel.MyProperty},
set: { p in self.viewModel.MyProperty = p})
{ SomeModalView()} }
这篇关于SwiftUI - 反转布尔绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文