如何在另一个视图中更改 class 的布尔实例?
How to change a boolean instance of a class in another view?
我想在一个视图中点击一个按钮并更改另一个视图中的文本。
更改布尔值的按钮在 LessonOne 上,我想在 ContentView 中显示的文本。
我第一课的代码是:
import SwiftUI
import Combine
class LessonTwoButton: ObservableObject {
let didChange = PassthroughSubject <Void, Never >()
var ButtonIsTapped = false {didSet {didChange.send()}}
}
struct LessonOne: View {
@ObservedObject var ActivateNextLesson = LessonTwoButton ()
var body: some View {
VStack{
Button (action: {
self.ActivateNextLesson.ButtonIsTapped.toggle()
}
, label: {
Text("Activate the next lesson")
})
}
}
}
我的 ContentView 中的代码是:
import SwiftUI
import Combine
struct ContentView: View {
@ObservedObject var lessontwobutton = LessonTwoButton()
var body: some View {
NavigationView{
VStack{
Text("Hello, world!")
.padding()
if lessontwobutton.ButtonIsTapped{
Text ("The button has been pressed. Your lesson is unlocked")}
else{
Text("This lesson is locked")
}
NavigationLink (
destination: LessonOne(),
label: {
Text("Next view")
})
}
}
}
}
代码运行,但它只是没有绑定...我继续 LessonOne,点击按钮,返回到 ContentView,没有任何改变。你能告诉我该怎么做吗?
您需要在两个视图中使用相同的 LessonTwoButton
实例:
struct ContentView: View {
@ObservedObject var lessontwobutton = LessonTwoButton() // create in parent only
...
struct LessonOne: View {
@ObservedObject var ActivateNextLesson: LessonTwoButton // declare only
...
}
NavigationLink(
destination: LessonOne(ActivateNextLesson: lessontwobutton), // pass here
label: {
Text("Next view")
}
)
而LessonTwoButton
可以简化为:
class LessonTwoButton: ObservableObject {
@Published var ButtonIsTapped = false
}
我想在一个视图中点击一个按钮并更改另一个视图中的文本。 更改布尔值的按钮在 LessonOne 上,我想在 ContentView 中显示的文本。
我第一课的代码是:
import SwiftUI
import Combine
class LessonTwoButton: ObservableObject {
let didChange = PassthroughSubject <Void, Never >()
var ButtonIsTapped = false {didSet {didChange.send()}}
}
struct LessonOne: View {
@ObservedObject var ActivateNextLesson = LessonTwoButton ()
var body: some View {
VStack{
Button (action: {
self.ActivateNextLesson.ButtonIsTapped.toggle()
}
, label: {
Text("Activate the next lesson")
})
}
}
}
我的 ContentView 中的代码是:
import SwiftUI
import Combine
struct ContentView: View {
@ObservedObject var lessontwobutton = LessonTwoButton()
var body: some View {
NavigationView{
VStack{
Text("Hello, world!")
.padding()
if lessontwobutton.ButtonIsTapped{
Text ("The button has been pressed. Your lesson is unlocked")}
else{
Text("This lesson is locked")
}
NavigationLink (
destination: LessonOne(),
label: {
Text("Next view")
})
}
}
}
}
代码运行,但它只是没有绑定...我继续 LessonOne,点击按钮,返回到 ContentView,没有任何改变。你能告诉我该怎么做吗?
您需要在两个视图中使用相同的 LessonTwoButton
实例:
struct ContentView: View {
@ObservedObject var lessontwobutton = LessonTwoButton() // create in parent only
...
struct LessonOne: View {
@ObservedObject var ActivateNextLesson: LessonTwoButton // declare only
...
}
NavigationLink(
destination: LessonOne(ActivateNextLesson: lessontwobutton), // pass here
label: {
Text("Next view")
}
)
而LessonTwoButton
可以简化为:
class LessonTwoButton: ObservableObject {
@Published var ButtonIsTapped = false
}