在两个视图之间传递数据 [英] Passing data between two views

查看:49
本文介绍了在两个视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 watchOS 6 上创建一个安静的简单应用程序,但是在 Apple 更改了 Xcode 11 beta 5 中的 ObjectBindig 之后,我的应用程序不再运行.我只是想在两个视图之间同步数据.

I wanted to create quiet a simple app on watchOS 6, but after Apple has changed the ObjectBindig in Xcode 11 beta 5 my App does not run anymore. I simply want to synchronize data between two Views.

所以我用新的@Published 重写了我的应用程序,但我无法真正设置它:

So I have rewritten my App with the new @Published, but I can't really set it up:

class UserInput: ObservableObject {

    @Published var score: Int = 0
}

struct ContentView: View {
    @ObservedObject var input = UserInput()
    var body: some View {
        VStack {
            Text("Hello World\(self.input.score)")
            Button(action: {self.input.score += 1})
                {
                    Text("Adder")
                }
            NavigationLink(destination: secondScreen()) {
                Text("Next View")
            }

        }

    }
}

struct secondScreen: View {
    @ObservedObject var input = UserInput()
    var body: some View {
        VStack {
            Text("Button has been pushed \(input.score)")
            Button(action: {self.input.score += 1
            }) {
                Text("Adder")
            }
        }

    }
}

推荐答案

您的代码有几个错误:

1) 您没有将 ContentView 放在 NavigationView 中,因此两个视图之间的导航从未发生.

1) You didn't put your ContentView in a NavigationView, so the navigation between the two views never happened.

2) 您以错误的方式使用了数据绑定.如果您需要第二个视图依赖属于第一个视图的某些状态,则需要将对该状态的绑定传递给第二个视图.在您的第一个视图和第二个视图中,您都内联创建了一个 @ObservedObject:

2) You used data binding in a wrong way. If you need the second view to rely on some state belonging to the first view you need to pass a binding to that state to the second view. Both in your first view and in your second view you had an @ObservedObject created inline:

@ObservedObject var input = UserInput()

因此,第一个视图和第二个视图使用两个完全不同的对象.相反,您有兴趣在视图之间共享 score.让第一个视图拥有 UserInput 对象,并将对分数整数的绑定传递给第二个视图.这样,两个视图将使用相同的值(您可以复制粘贴下面的代码并自己尝试).

so, the first view and the second one worked with two totally different objects. Instead, you are interested in sharing the score between the views. Let the first view own the UserInput object and just pass a binding to the score integer to the second view. This way both the views will work on the same value (you can copy paste the code below and try yourself).

import SwiftUI

class UserInput: ObservableObject {
    @Published var score: Int = 0
}

struct ContentView: View {
    @ObservedObject var input = UserInput()
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World\(self.input.score)")
                Button(action: {self.input.score += 1})
                    {
                        Text("Adder")
                    }
                NavigationLink(destination: secondScreen(score: self.$input.score)) {
                    Text("Next View")
                }

            }
        }

    }
}

struct secondScreen: View {
    @Binding var score:  Int
    var body: some View {
        VStack {
            Text("Button has been pushed \(score)")
            Button(action: {self.score += 1
            }) {
                Text("Adder")
            }
        }

    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

如果你真的需要它,你甚至可以将整个 UserInput 对象传递给第二个视图:

If you really need it you can even pass the entire UserInput object to the second view:

import SwiftUI

class UserInput: ObservableObject {
    @Published var score: Int = 0
}

struct ContentView: View {
    @ObservedObject var input = UserInput() //please, note the difference between this...
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World\(self.input.score)")
                Button(action: {self.input.score += 1})
                    {
                        Text("Adder")
                    }
                NavigationLink(destination: secondScreen(input: self.input)) {
                    Text("Next View")
                }

            }
        }

    }
}

struct secondScreen: View {
    @ObservedObject var input: UserInput //... and this!
    var body: some View {
        VStack {
            Text("Button has been pushed \(input.score)")
            Button(action: {self.input.score += 1
            }) {
                Text("Adder")
            }
        }

    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

这篇关于在两个视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆