-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day15-Stepper+Alert.swift
78 lines (66 loc) · 2.53 KB
/
Day15-Stepper+Alert.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
//
// ContentView.swift
// BetterRest
//
// Created by Dali Han on 2020/8/3.
// Copyright © 2020 Dali Han. All rights reserved.
// Credit: Paul Hudson
// Link: https://www.hackingwithswift.com/books/ios-swiftui/betterrest-introduction
import SwiftUI
struct ContentView: View {
@State private var wakeUp = Date()
@State private var sleepAmount = 8.0
@State private var coffeeAmount = 1
@State private var showAlert = false
@State private var alertTitle = "Alert Title"
@State private var alertMsg = "Alert Msg Content"
var body: some View {
NavigationView {
Form {
VStack(alignment: .leading, spacing: 0) {
Text("When do you want to wake up?")
.font(.headline)
DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
.datePickerStyle(WheelDatePickerStyle())
.labelsHidden()
}
VStack(alignment: .leading, spacing: 0) {
Text("Desired amount of sleep")
.font(.headline)
Stepper(value: $sleepAmount, in: 4...12, step: 0.25) {
Text("\(sleepAmount, specifier: "%g") hours")
}
}
VStack(alignment: .leading, spacing: 0) {
Text("Daily Coffee intake")
.font(.headline)
Stepper(value: $coffeeAmount, in: 1...20) {
if coffeeAmount == 1 {
Text("1 cup")
} else {
Text("\(coffeeAmount) cups")
}
}
}
Button("Alert") {
self.showAlert.toggle()
}
}
.navigationBarTitle("Better Sleep")
.navigationBarItems(trailing:
Button(action: {
print("Button Tapped")
}) {
Text("Calculate")
})
.alert(isPresented: $showAlert) {
Alert(title: Text(alertTitle), message: Text(alertMsg), dismissButton: .default(Text("OK")))
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}