Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI:将绑定转换为另一个绑定_Swiftui - Fatal编程技术网

SwiftUI:将绑定转换为另一个绑定

SwiftUI:将绑定转换为另一个绑定,swiftui,Swiftui,是否有一种方法,例如从逻辑上否定绑定?例如,我有一个状态变量 @State var isDone=true 我将其作为投标书传递到不同的子视图中。然后我想使用它,例如在导航链接中使用isActive,这样它只在而不是isDone时显示: NavigationLink(目的地:…,isActive:!self.$isDone)//isNotDone,但它在许多上下文中都是不自然的。那么,有没有简单的方法来实现布尔绑定的逆呢 如果我理解正确,您需要: extension Binding where

是否有一种方法,例如从逻辑上否定
绑定
?例如,我有一个状态变量

@State var isDone=true
我将其作为投标书传递到不同的子视图中。然后我想使用它,例如在
导航链接
中使用
isActive
,这样它只在
而不是isDone
时显示:


NavigationLink(目的地:…,isActive:!self.$isDone)//isNotDone
,但它在许多上下文中都是不自然的。那么,有没有简单的方法来实现布尔绑定的逆呢

如果我理解正确,您需要:

extension Binding where Value == Bool {
    public func negate() -> Binding<Bool> {
        return Binding<Bool>(get:{ !self.wrappedValue }, 
            set: { self.wrappedValue = !$0})
    }
}

struct TestInvertBinding: View {
    @State var isDone = true
    var body: some View {
        NavigationView {
            NavigationLink(destination: Text("Details"), 
                isActive: self.$isDone.negate()) {
                Text("Navigate")
            }
        }
    }
}

struct TestInvertBinding_Previews: PreviewProvider {
    static var previews: some View {
        TestInvertBinding()
    }
}
扩展绑定,其中Value==Bool{
public func negate()->绑定{
返回绑定(get:{!self.wrappedValue},
集合:{self.wrappedValue=!$0})
}
}
结构TestInvertBinding:视图{
@状态变量isDone=true
var body:一些观点{
导航视图{
导航链接(目的地:文本(“详细信息”),
isActive:self.$isDone.negate()){
文本(“导航”)
}
}
}
}
结构TestInvertBinding\u预览:PreviewProvider{
静态var预览:一些视图{
TestInvertBinding()
}
}