Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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多绑定_Swift_Xcode_Swiftui - Fatal编程技术网

SwiftUI多绑定

SwiftUI多绑定,swift,xcode,swiftui,Swift,Xcode,Swiftui,我有一个MasterView控制器,其中包含一个列表和我的详细视图控制器。局部视图控制器将编辑有关在MasterView中选择的数据集的信息 据我所知,我必须在我的详细信息中创建一个绑定,因此当我编辑详细信息时,我的MasterView列表中的详细信息也应该更改 正确的做法是什么?“我的详细信息”视图也包含多个视图,可以在其中进行编辑。我是否必须将信息绑定到所有较低的视图?我可以从@State->@Binding->@Binding转到第一个状态吗 我正在为MacOS使用SwiftUI 这就是它

我有一个MasterView控制器,其中包含一个列表和我的详细视图控制器。局部视图控制器将编辑有关在MasterView中选择的数据集的信息

据我所知,我必须在我的详细信息中创建一个绑定,因此当我编辑详细信息时,我的MasterView列表中的详细信息也应该更改

正确的做法是什么?“我的详细信息”视图也包含多个视图,可以在其中进行编辑。我是否必须将信息绑定到所有较低的视图?我可以从
@State->@Binding->@Binding
转到第一个状态吗

我正在为MacOS使用SwiftUI

这就是它的外观(左侧为列表视图,右侧为编辑视图)

对于任何更复杂的操作,如简单的状态/绑定来直观地更改UI的某些组件,请继续使用@observeObject/@ObservedObject,或者使用@State/@Binding来实现真正的全局共享@EnvironmentObject。把所有的逻辑都放在你的模型上,不要试图在你的SwiftUI中这样做。它将为您节省大量的麻烦、bug查找等

更新没有比查看文档更好的方法了

/// A linked View property that reads a `ObservableObject` supplied by an
/// ancestor view that will automatically invalidate its view when the object
/// changes.
///
/// - Precondition: A model must be provided on an ancestor view by calling
///     `environmentObject(_:)`.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper public struct EnvironmentObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {

    /// A wrapper of the underlying `ObservableObject` that can create
    /// `Binding`s to its properties using dynamic member lookup.
    @dynamicMemberLookup public struct Wrapper {

        /// Creates a `Binding` to a value semantic property of a
        /// reference type.
        ///
        /// If `Value` is not value semantic, the updating behavior for
        /// any views that make use of the resulting `Binding` is
        /// unspecified.
        public subscript<Subject>(dynamicMember keyPath: ReferenceWritableKeyPath<ObjectType, Subject>) -> Binding<Subject> { get }
    }

    /// The current model supplied by an ancestor view.
    @inlinable public var wrappedValue: ObjectType { get }

    public var projectedValue: EnvironmentObject<ObjectType>.Wrapper { get }

    public init()
}

谢谢我现在正在使用EnvironmentObject。但是,如何在导航视图中使用它。我有一个被选中的人,我通过一个州获得。但我想将Environment Person对象发送给它,而不是所选对象。要查看ObservedObject和EnvironmentalObject之间的区别,我在回答中添加了对文档的引用。
/// A dynamic view property that subscribes to a `ObservableObject` automatically invalidating the view
/// when it changes.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper public struct ObservedObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject {

    /// A wrapper of the underlying `ObservableObject` that can create
    /// `Binding`s to its properties using dynamic member lookup.
    @dynamicMemberLookup public struct Wrapper {

        /// Creates a `Binding` to a value semantic property of a
        /// reference type.
        ///
        /// If `Value` is not value semantic, the updating behavior for
        /// any views that make use of the resulting `Binding` is
        /// unspecified.
        public subscript<Subject>(dynamicMember keyPath: ReferenceWritableKeyPath<ObjectType, Subject>) -> Binding<Subject> { get }
    }

    public init(initialValue: ObjectType)

    public init(wrappedValue: ObjectType)

    public var wrappedValue: ObjectType

    public var projectedValue: ObservedObject<ObjectType>.Wrapper { get }
}
/// A type of object with a publisher that emits before the object has changed.
///
/// By default an `ObservableObject` will synthesize an `objectWillChange`
/// publisher that emits before any of its `@Published` properties changes:
///
///     class Contact: ObservableObject {
///         @Published var name: String
///         @Published var age: Int
///
///         init(name: String, age: Int) {
///             self.name = name
///             self.age = age
///         }
///
///         func haveBirthday() -> Int {
///             age += 1
///             return age
///         }
///     }
///
///     let john = Contact(name: "John Appleseed", age: 24)
///     john.objectWillChange.sink { _ in print("\(john.age) will change") }
///     print(john.haveBirthday())
///     // Prints "24 will change"
///     // Prints "25"
///
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public protocol ObservableObject : AnyObject {

    /// The type of publisher that emits before the object has changed.
    associatedtype ObjectWillChangePublisher : Publisher = ObservableObjectPublisher where Self.ObjectWillChangePublisher.Failure == Never

    /// A publisher that emits before the object has changed.
    var objectWillChange: Self.ObjectWillChangePublisher { get }
}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension ObservableObject where Self.ObjectWillChangePublisher == ObservableObjectPublisher {

    /// A publisher that emits before the object has changed.
    public var objectWillChange: ObservableObjectPublisher { get }
}