【问题标题】:SwiftUI doubly nested NavigationLink views not responding to changing ObservedObjectSwiftUI 双重嵌套 NavigationLink 视图不响应更改的 ObservedObject
【发布时间】:2020-02-27 16:38:02
【问题描述】:

我创建了三个视图。我按顺序将 ObservedObject 状态传递给每个视图。当我更改最后一个视图 (AnotherView2) 中的状态时,我的应用程序不会显示“是的,它已完成!”的文本视图。但是,如果我在 AnotherView 中取消注释这一行

self.userDefaultsManager.setupComplete = true

通过显示文本,它可以按预期工作。


struct ContentView: View {

    @ObservedObject var userDefaultsManager = UserDefaultsManager()
    @State var showAnotherView: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                if !userDefaultsManager.setupComplete {
                    Button(action: {
                        self.showAnotherView.toggle()
                    }) {
                        Text("Show Another View")
                    }
                    NavigationLink(destination: AnotherView(userDefaultsManager: userDefaultsManager), isActive: $showAnotherView, label: {
                        EmptyView()
                    })
                } else {
                    Text("YES IT IS FINISHED!")
                }

            }
        }
    }
}


struct AnotherView: View {

    @ObservedObject var userDefaultsManager: UserDefaultsManager
    @State var showAnotherView2: Bool = false

    var body: some View {
        VStack {
            Button(action: {
                //self.userDefaultsManager.setupComplete = true
                self.showAnotherView2 = true
            }, label: {
                Text("Press")
            })
            NavigationLink(destination: AnotherView2(userDefaultsManager: userDefaultsManager), isActive: $showAnotherView2, label: {
                EmptyView()
            })
        }
    }
}


struct AnotherView2: View {

    @ObservedObject var userDefaultsManager: UserDefaultsManager

    var body: some View {
        Button(action: {
            self.userDefaultsManager.setupComplete = true
        }, label: {
            Text("Just Do It")
        })
    }
}


class UserDefaultsManager: ObservableObject {

    @Published var setupComplete: Bool = UserDefaults.standard.bool(forKey: "setupComplete") {
        didSet { UserDefaults.standard.set(self.setupComplete, forKey: "setupComplete") }
    }
}

有人可以帮助我了解我的代码或 API 有什么问题,它不能用于以这种方式显示视图的双嵌套调用吗?

编辑:使用 XCode 11.3 和 iOS 13.3.1

【问题讨论】:

  • 在 Xcode 11.2 / iOS 13.2 中运行良好
  • 我的意思是状态已经改变,但如果你的意思是自动展开到根目录,那么在你的第二种(取消注释)情况下,它是副作用,相当不受欢迎,实际上现在不支持。

标签: swiftui-navigationlink swiftui


【解决方案1】:

我想这是一个提供所需行为的布局

var body: some View {
    Group {
        if !userDefaultsManager.setupComplete {
            NavigationView {
                VStack {
                    Button(action: {
                        self.showAnotherView.toggle()
                    }) {
                        Text("Show Another View")
                    }
                    NavigationLink(destination: AnotherView(userDefaultsManager: userDefaultsManager), isActive: $showAnotherView, label: {
                        EmptyView()
                    })
                }
            }
        } else {
            Text("YES IT IS FINISHED!")
        }
    }
}

【讨论】:

  • 您能否指出一个更好的解决方案来解决“如何呈现在用户看到后被忽略为另一个视图序列的一系列视图?”的一般问题?似乎有一种方法更适合 SwiftUI 或一般的反应式编程。
猜你喜欢
  • 2020-08-26
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 2019-12-24
  • 1970-01-01
  • 2020-12-21
相关资源
最近更新 更多