-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestionsView.swift
100 lines (80 loc) · 2.71 KB
/
QuestionsView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// QuestionsView.swift
// sleep_tracker
//
// Created by Benicio Nell on 05.01.25.
//
import SwiftUI
struct QuestionsView: View {
@State var questions: [Question]
var body: some View {
NavigationStack {
Text("Täglicher Fragebogen!")
.font(.title)
List {
ForEach($questions) { $question in
HStack {
Text(question.text)
Spacer()
Picker("", selection: $question.answer, content: {
Text("Nein").tag(false)
Text("Ja").tag(true)
})
.pickerStyle(.segmented)
.frame(maxWidth: 130)
}
}
NavigationLink(destination: {
AddQuestionView()
}, label: {
Text("Weitere Fragen hinzufügen")
.foregroundStyle(.blue)
.fontWeight(.bold)
})
}
}
}
}
class Question: Identifiable {
var id: UUID = UUID()
var text: String
var answer: Bool = false
init(text: String, answer: Bool) {
self.text = text
self.answer = answer
}
}
struct AddQuestionView: View {
@State var question: Question = Question(text: "", answer: false)
var body: some View {
Text("Neue Frage")
.font(.title)
Form {
TextField("Neue Frage", text: $question.text)
HStack {
Text("Standard Antwort:")
Spacer()
Picker("", selection: $question.answer, content: {
Text("Ja").tag(true)
Text("Nein").tag(false)
})
.pickerStyle(.segmented)
.frame(maxWidth: 140)
}
Text("Fertig")
.foregroundStyle(.blue)
Text("Vorschläge:")
}
}
}
#Preview("Fragebogen") {
let q1: Question = Question(text: "Alkohol getrunken?", answer: false)
let q3: Question = Question(text: "Unmittelbar vor dem schlafengehen gegessen?", answer: false)
let q2: Question = Question(text: "Stressvollen Tag gehabt und noch was anderes?", answer: false)
let q4: Question = Question(text: "Mittagsschlaf", answer: false)
let q5: Question = Question(text: "Sport gemacht?", answer: true)
QuestionsView(questions: [q1,q2,q3,q4,q5])
}
#Preview("Neue Frage") {
AddQuestionView()
}