Day 35 of #100DaysOfSwiftUI

Start 100DaysOfSwiftUI from 2020.Mar.18th.

Day 35: Milestone: Projects 1-3
done 25min + 30min

New findings: followings are new findings : none because it is challenge day

Code for final(?) version

    Features

  • if answer is wrong, text will be changed to red
  • no way to restart....
struct ContentView: View {
    @State private var underSetting = true
    @State private var whichSeries:Int = 1
    @State private var howManyQuestions:Int = 5

    // for play
    private var baseListForQuestions:[Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    @State private var listForQuestions:[Int] = []

    var body: some View {
        Group {
            if underSetting {
                SettingView(underSetting: $underSetting,
                            whichSeries: $whichSeries,
                            howManyQuestions: $howManyQuestions)
            } else {
                PlayView(series: $whichSeries,
                         listForQuestions: $listForQuestions)
                .onAppear(perform: makeQuestions)
            }
        }
    }
    
    func makeQuestions() {
        listForQuestions = []
        
        for _ in 1...howManyQuestions {
            let newQuiz = Int.random(in: 1...8)
            listForQuestions.append(baseListForQuestions[newQuiz])
        }
        return
    }
}

struct PlayView: View {
    @Binding var series:Int
    @Binding var listForQuestions:[Int]
    @State var answeres:[String] = ["", "", "", "", "", "", "", "", "", ""]
    @State var correctFlags:[Bool] = [true, true, true, true, true, true, true, true, true, true]
    var body: some View {
        VStack {
            Text("Playing")
            ForEach(0..

code for setting view (very basic version)

struct ContentView: View {
    @State private var underSetting = true
    @State private var whichSeries:Int = 1
    @State private var howManyQuestions:Int = 5

    var body: some View {
        Group {
            if underSetting {
                SettingView(underSetting: $underSetting,
                            whichSeries: $whichSeries,
                            howManyQuestions: $howManyQuestions)
            } else {
                PlayView()
            }
        }
    }
}

struct PlayView: View {
    var body: some View {
        Text("Playing")
    }
}

struct SettingView: View {
    @Binding var underSetting: Bool
    @Binding var whichSeries:Int
    @Binding var howManyQuestions:Int
    var body: some View {
        Form {
            Text("Setting")
            Stepper("which series  \(whichSeries)", value: $whichSeries, in: 1...9)
            Stepper("how many questions : \(howManyQuestions)", value: $howManyQuestions,
                    in: 1...10)
            Button(action: {
                self.underSetting = false
            }) {
                Text("setting done")
            }
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です