首页 > 解决方案 > 如何在 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)
    }
}

标签: swiftuiswiftui-view

解决方案


我不确定您在哪里得到错误,因为可以将变量传递到另一个视图。我修改了您的代码以说明我认为您正在努力实现的目标。

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())
        }

    }
}

推荐阅读