swiftui - 如何在 swiftui 中为子视图设置私有状态变量?
问题描述
所以我正在创建这个应用程序,用户可以在其中选择他希望练习的乘法表,以及第一个视图上的问题数量,然后按下按钮,
它继续传递这些数据的下一个视图,然后它将创建一个问题库供他做
有一个错误说我不能传递一个私有变量,为什么会这样?我已附上我的代码以供参考
import SwiftUI
struct ContentView: View {
@State private var multiplicationTable = 1
@State private var amountQuestions = 1
let multiplicationTables = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let amountQuestionss = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var body: some View {
NavigationView {
VStack {
Picker(selection: $multiplicationTable, label: Text("multiplicationTable")) {
ForEach(0 ..< 10) {num in
Text("\(multiplicationTables[num])")
}
}
.padding()
.padding()
Text("Choose number of Questions")
Picker("Select number of questions", selection: $amountQuestions) {
ForEach(0 ..< 10) {num in
Text("\(amountQuestionss[num])")
}
}
NavigationLink(destination: kek(One: multiplicationTable, Two: amountQuestions) .navigationBarHidden(true)) {
Button ("GO") {
}
.padding(50)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
}
.navigationTitle("Choose Multiplication Table")
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是第一个视图,这是第二个视图
import SwiftUI
struct kek: View {
let One : Int
let Two : Int
@State var Question : String
@State var answer = ""
@State var Three = 0
@State var Four = 0
func nextQuestion(){
Three = One
Four = Int.random(in: 0...10)
Question = "\(Three) * \(Four)"
}
var body: some View {
VStack {
Text("Question: What is \(Question)?")
Form {
Section {
TextField("Amount", text: $answer)
.keyboardType(.decimalPad)
}
}
Button ("Next") {
}
.padding(50)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
}
}
}
struct kek_Previews: PreviewProvider {
static var previews: some View {
kek(One: 1, Two: 2)
}
}
解决方案
我不确定您在哪里得到错误,因为可以将变量传递到另一个视图。我修改了您的代码以说明我认为您正在努力实现的目标。
struct ContentView: View {
@State private var multiplicationTable = 1
@State private var amountQuestions = 1
let multiplicationTables = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let numberOfQuestions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var body: some View {
NavigationView {
VStack {
Picker("Multiplication Table", selection: $multiplicationTable) {
// There was an "off by one" issue in the kek View which I resolved by using the value of the array itself for the selection. Not the location in the array.
ForEach(multiplicationTables, id: \.self) { num in
Text("\(num)")
}
}
.pickerStyle(.segmented)
.padding([.top, .bottom], 20)
/*
Don't use
.padding()
.padding()
Instead you can specify how large you'd like the padding to be around the view.
*/
Text("Choose number of Questions")
Picker("Select number of questions", selection: $amountQuestions) {
ForEach(0 ..< numberOfQuestions.count) {num in
Text("\(numberOfQuestions[num])")
}
}
NavigationLink {
kek(one: multiplicationTable, two: amountQuestions)
} label: {
// A NavigationLink is clickable by default, so you don't need to place a button inside of it.
// Here I used Text and formatted it just as you had with your Button.
Text("Go")
.padding(50)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
}
.navigationTitle("Choose Multiplication Table")
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
struct kek: View {
let one: Int
let two: Int // note: It doesn't look like you're actually using this property. Consider removing it.
@State private var answer = ""
@State private var randomNumber = 0
@State private var three = 0
@State private var question = ""
func nextQuestion(){
three = one
randomNumber = Int.random(in: 0...10)
question = "\(three) * \(randomNumber)"
}
var body: some View {
VStack {
Text("Question: What is \(question)?")
Form {
Section {
TextField("Amount", text: $answer)
.keyboardType(.decimalPad)
}
}
Button ("Next") {
nextQuestion()
}
.padding(50)
.background(Color.red)
.foregroundColor(.white)
.clipShape(Circle())
}
}
}
推荐阅读
- java - 如何以编程方式检查“自动日期和时间”中是否选择了“使用 GPS 提供的时间”?
- amazon-web-services - AWS S3 托管网站的附加 DNS 名称
- vim - 如何将双引号 (") 映射到 vimrc 中的某些内容
- laravel - Laravel 向控制器提交数据,然后将数据发送到操作 URL
- django-models - Django ORM 以最少的 SQL 查询从数据库中获取模型的嵌套树
- python - 试图从 /nix/store 中删除一个包,现在系统出现错误,如何修复?
- java - 如何使用 JUnit 扩展将 @BeforeEach 与 @Test 方法链接?
- r - 以 2、5、10、25 的间隔创建一个从 -100 到 100 的数字序列。如何在 R 中做到这一点
- sql - 根据索引范围将一列变为多列
- php - 使用 modal bootstrap 和 php 创建更新表单