这篇文章介绍了SwiftUI学习笔记中的一个复习项目,涉及滑块Picker和保留两位小数的知识点。文章提供了完整的代码和SwiftPlayground进度。
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
投诉重新开始复习前面的东西。
滑块Picker
.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
|
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进度