SwiftUI - 将变量整数值带到其他视图
SwiftUI - Bring variable integer value to other views
我做了一个小测验。当你按下一个按钮时,一个特定的数量会添加到 Xcode 和 Ycode。
现在我想把 Xcode 和 Ycode 的值带到下一个视图。
因此,当您在下一个视图中时,您可以回答另一个问题,并且在现有金额之上添加一个金额,然后我想将该值带到另一个视图中。
那么如何将Xcode和Ycode的值带到下一个视图呢?
这是我制作的代码:
@State var ShowButton: Bool = false
@State var ButtonYes: Bool = false
@State var ButtonNo: Bool = false
@State var ButtonSometimes: Bool = false
@State var Xcode = 0
@State var Ycode = 0
var body: some View {
ZStack{
Image("Chimps")
.resizable()
.ignoresSafeArea()
.navigationBarHidden(true)
VStack{
Text("Question 1")
.font(.largeTitle)
.fontWeight(.heavy)
.padding()
.foregroundColor(.white)
.background(Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
Spacer()
Text(String(Xcode))
Text(String(Ycode))
Text("Question 1")
.foregroundColor(Color.white)
.font(.headline)
.padding()
.background(Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
Spacer()
Spacer()
HStack(spacing:10) {
Button("Yes") {
ShowButton = true
ButtonYes.toggle()
if ButtonYes == true {
Xcode += 5
}
if ButtonYes == true {
Ycode += 5
}
if ButtonYes == false {
Xcode -= 5
}
if ButtonYes == false {
Ycode -= 5
}
if ButtonNo == true {
Xcode -= 3
}
if ButtonNo == true {
Ycode -= 3
}
if ButtonSometimes == true {
Xcode -= 1
}
if ButtonSometimes == true {
Ycode -= 1
}
if ButtonYes == true {
ButtonNo = false
}
if ButtonYes == true {
ButtonSometimes = false
}
}
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.white)
.padding()
.background(ButtonYes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
.shadow(color: .black, radius: 10, x: 10, y: 10)
Button("No") {
ShowButton = true
ButtonNo.toggle()
if ButtonNo == true {
Xcode += 3
}
if ButtonNo == true {
Ycode += 3
}
if ButtonNo == false {
Xcode -= 3
}
if ButtonNo == false {
Ycode -= 3
}
if ButtonYes == true {
Xcode -= 5
}
if ButtonYes == true {
Ycode -= 5
}
if ButtonSometimes == true {
Xcode -= 1
}
if ButtonSometimes == true {
Ycode -= 1
}
if ButtonNo == true {
ButtonYes = false
}
if ButtonNo == true {
ButtonSometimes = false
}
}
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.white)
.padding()
.background(ButtonNo ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
.shadow(color: .black, radius: 10, x: 10, y: 10)
Button("Sometimes") {
ShowButton = true
ButtonSometimes.toggle()
if ButtonSometimes == true {
Xcode += 1
}
if ButtonSometimes == true {
Ycode += 1
}
if ButtonSometimes == false {
Xcode -= 1
}
if ButtonSometimes == false {
Ycode -= 1
}
if ButtonYes == true {
Xcode -= 5
}
if ButtonYes == true {
Ycode -= 5
}
if ButtonNo == true {
Xcode -= 3
}
if ButtonNo == true {
Ycode -= 3
}
if ButtonSometimes == true {
ButtonYes = false
}
if ButtonSometimes == true {
ButtonNo = false
}
}
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.white)
.padding()
.background(ButtonSometimes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
.shadow(color: .black, radius: 10, x: 10, y: 10)
}
Spacer()
if ShowButton {
NavigationLink(
destination: Question2(),
label: {
Rectangle()
.fill(Color(red: 0.493, green: 0.184, blue: 0.487))
.frame(width: 150, height: 80, alignment: .bottom)
.cornerRadius(20)
.padding(10)
.shadow(color: .black, radius: 10, x: 10, y: 10)
.overlay(
Text("Question 2")
.font(.largeTitle)
.foregroundColor(.white)
.shadow(color: .black, radius: 10, x: 10, y: 10)
)}
)
Spacer()
}
}
}
}
}
这是一个快速有效的示例,说明如何在第二个屏幕上使用 @Binding 来更新现有数量。但我建议您查看文档并了解何时使用@State、@Binding、@ObservedObject、@StateObject 等。
import SwiftUI
struct FirstView: View {
@State var xValue = 0
@State var yValue = 0
var body: some View {
NavigationView {
VStack {
Text("\(xValue)")
Text("\(yValue)")
Button("add to x") {
xValue += 1
}
Button("add to y") {
yValue += 1
}
Divider()
NavigationLink("next screen", destination: SecondView(xValue: $xValue, yValue: $yValue))
}
}
}
}
struct SecondView: View {
@Binding var xValue: Int
@Binding var yValue: Int
var body: some View {
NavigationView {
VStack {
Text("\(xValue)")
Text("\(yValue)")
Button("add to x") {
xValue += 1
}
Button("add to y") {
yValue += 1
}
}
}
}
}
我做了一个小测验。当你按下一个按钮时,一个特定的数量会添加到 Xcode 和 Ycode。 现在我想把 Xcode 和 Ycode 的值带到下一个视图。 因此,当您在下一个视图中时,您可以回答另一个问题,并且在现有金额之上添加一个金额,然后我想将该值带到另一个视图中。
那么如何将Xcode和Ycode的值带到下一个视图呢? 这是我制作的代码:
@State var ShowButton: Bool = false
@State var ButtonYes: Bool = false
@State var ButtonNo: Bool = false
@State var ButtonSometimes: Bool = false
@State var Xcode = 0
@State var Ycode = 0
var body: some View {
ZStack{
Image("Chimps")
.resizable()
.ignoresSafeArea()
.navigationBarHidden(true)
VStack{
Text("Question 1")
.font(.largeTitle)
.fontWeight(.heavy)
.padding()
.foregroundColor(.white)
.background(Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
Spacer()
Text(String(Xcode))
Text(String(Ycode))
Text("Question 1")
.foregroundColor(Color.white)
.font(.headline)
.padding()
.background(Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
Spacer()
Spacer()
HStack(spacing:10) {
Button("Yes") {
ShowButton = true
ButtonYes.toggle()
if ButtonYes == true {
Xcode += 5
}
if ButtonYes == true {
Ycode += 5
}
if ButtonYes == false {
Xcode -= 5
}
if ButtonYes == false {
Ycode -= 5
}
if ButtonNo == true {
Xcode -= 3
}
if ButtonNo == true {
Ycode -= 3
}
if ButtonSometimes == true {
Xcode -= 1
}
if ButtonSometimes == true {
Ycode -= 1
}
if ButtonYes == true {
ButtonNo = false
}
if ButtonYes == true {
ButtonSometimes = false
}
}
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.white)
.padding()
.background(ButtonYes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
.shadow(color: .black, radius: 10, x: 10, y: 10)
Button("No") {
ShowButton = true
ButtonNo.toggle()
if ButtonNo == true {
Xcode += 3
}
if ButtonNo == true {
Ycode += 3
}
if ButtonNo == false {
Xcode -= 3
}
if ButtonNo == false {
Ycode -= 3
}
if ButtonYes == true {
Xcode -= 5
}
if ButtonYes == true {
Ycode -= 5
}
if ButtonSometimes == true {
Xcode -= 1
}
if ButtonSometimes == true {
Ycode -= 1
}
if ButtonNo == true {
ButtonYes = false
}
if ButtonNo == true {
ButtonSometimes = false
}
}
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.white)
.padding()
.background(ButtonNo ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
.shadow(color: .black, radius: 10, x: 10, y: 10)
Button("Sometimes") {
ShowButton = true
ButtonSometimes.toggle()
if ButtonSometimes == true {
Xcode += 1
}
if ButtonSometimes == true {
Ycode += 1
}
if ButtonSometimes == false {
Xcode -= 1
}
if ButtonSometimes == false {
Ycode -= 1
}
if ButtonYes == true {
Xcode -= 5
}
if ButtonYes == true {
Ycode -= 5
}
if ButtonNo == true {
Xcode -= 3
}
if ButtonNo == true {
Ycode -= 3
}
if ButtonSometimes == true {
ButtonYes = false
}
if ButtonSometimes == true {
ButtonNo = false
}
}
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(.white)
.padding()
.background(ButtonSometimes ? Color(red: 0.272, green: 0.471, blue: 0.262) : Color(red: 0.493, green: 0.184, blue: 0.487))
.cornerRadius(20)
.shadow(color: .black, radius: 10, x: 10, y: 10)
}
Spacer()
if ShowButton {
NavigationLink(
destination: Question2(),
label: {
Rectangle()
.fill(Color(red: 0.493, green: 0.184, blue: 0.487))
.frame(width: 150, height: 80, alignment: .bottom)
.cornerRadius(20)
.padding(10)
.shadow(color: .black, radius: 10, x: 10, y: 10)
.overlay(
Text("Question 2")
.font(.largeTitle)
.foregroundColor(.white)
.shadow(color: .black, radius: 10, x: 10, y: 10)
)}
)
Spacer()
}
}
}
}
}
这是一个快速有效的示例,说明如何在第二个屏幕上使用 @Binding 来更新现有数量。但我建议您查看文档并了解何时使用@State、@Binding、@ObservedObject、@StateObject 等。
import SwiftUI
struct FirstView: View {
@State var xValue = 0
@State var yValue = 0
var body: some View {
NavigationView {
VStack {
Text("\(xValue)")
Text("\(yValue)")
Button("add to x") {
xValue += 1
}
Button("add to y") {
yValue += 1
}
Divider()
NavigationLink("next screen", destination: SecondView(xValue: $xValue, yValue: $yValue))
}
}
}
}
struct SecondView: View {
@Binding var xValue: Int
@Binding var yValue: Int
var body: some View {
NavigationView {
VStack {
Text("\(xValue)")
Text("\(yValue)")
Button("add to x") {
xValue += 1
}
Button("add to y") {
yValue += 1
}
}
}
}
}