重新开始复习前面的东西。

滑块Picker

-w371

.pickerStyle(SegmentedPickerStyle())

保留两位小数

Text("$ \(finalmon, specifier: "%.2f")")

代码

ContentView.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
//
// ContentView.swift
// Practise2-1
//
// Created by 张洪Hoo on 2020/3/29.
// Copyright © 2020 张洪Hoo. All rights reserved.
//

import SwiftUI

struct ContentView: View {
@State private var amount = ""
@State private var picknum = 1
@State private var tip = 20

var finalmon: Double {
let amount = Double(self.amount) ?? 0
let picknum = Double(self.picknum)
let tip = Double(self.tip)

let alltip = amount * tip / 100
let allmon = amount + alltip
let average = allmon / picknum

return average
}

let tipmoney = [10,15,20,25,0]


var body: some View {

NavigationView {
Form {
Text("Hello world")
TextField("Amount", text: $amount)
.keyboardType(.numberPad)

Section {
Picker(selection: $picknum, label: Text("选择人数")) {
ForEach(1..<100, id: \.self){ item in
Text("\(item) 人")
}
}
}

Section(header: Text("How much tip do you want to leave ?")) {
Picker(selection: $tip, label: Text("Picker")) {
ForEach(self.tipmoney, id: \.self){ item in
Text("\(item)%")
}
}.pickerStyle(SegmentedPickerStyle())
}

Section {
Text("$ \(finalmon, specifier: "%.2f")")
}
}
.navigationBarTitle(Text("WeSplit"))
}



}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

SwiftPlayground进度