I am creating a tip app that has you enter a bill total, a tip percentage from 3 predetermined UIButtons and then a stepper for selecting number of people to split bill between.
The problem I am having is if a tip percentage is not selected, I am not sure how to make the app default to selecting the 10% button so that the app still calculates properly with just a bill total entered.
Very new to programming, apologize if this doesn't completely make sense, any help is appreciated.
CodePudding user response:
@IBOutlet weak var yourButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
yourButton.isSelected = true
calculate()
}
func calculate() {
if yourButton.isSelected {
// tip calculate
}
}
I am beginner too. But I think I can help you
CodePudding user response:
Since you didn't provide your existing code (yet), it is even unclear if you are using UIKit or SwiftUI
If you have a Bill object (struct/class) or so, you can initialize the parameter with a default value, e.g. int value 10 or with an Enum.
SwiftUI
In a SwiftUI environment, I can imagine to have the following solution (with an Enum), here I am using buttons and the background color depends on if the assigned rawValue is set as tip in the instance of the Bill object.
Btw. you can also use a picker as a menu that opens or with the style .segmented.
//
// Bill.swift
// Bill
//
// Created by Sebastian on 25.08.22.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack() {
BillListView()
}
}
}
struct BillListView: View {
// TEST DATA START
// JUST FOR TESTING, I GUESS YOU GET YOUR DATA FROM SOMEWHERE ELSE
// YOUR DATA MAY LOOK DIFFERENT, ACCORDING TO YOUR NEEDS
func initializeTestValues() {
let steak = Item(name: "Steak", net: 22.00)
let fish = Item(name: "Fish", net: 22.00)
let burger = Item(name: "Burger", net: 18.00)
let fries = Item(name: "Fries", net: 4.50)
let potatoes = Item(name: "Potatoes", net: 3.20)
let coke = Item(name: "Coke", net: 3.20)
let beer = Item(name: "Beer", net: 4.00)
let wine = Item(name: "Wine", net: 5.00)
let billOne = Bill(items: [burger, fries, coke], orderNumber: 1001)
let billTwo = Bill(items: [steak, fries, beer], orderNumber: 1002)
let billThree = Bill(items: [fish, potatoes, wine], orderNumber: 1003)
bills.append(billOne)
bills.append(billTwo)
bills.append(billThree)
}
// TEST DATA END
// This could be moved into a ViewModel (Check out @ObservedObject and @Published)
@State var bills: [Bill] = []
var body: some View {
NavigationView() {
List() {
ForEach(bills.indices, id: \.self) { id in
NavigationLink(destination: BillView(bill: $bills[id])) {
Text("\(String(bills[id].orderNumber))")
}
}
}
.navigationTitle("All Orders")
}.onAppear(perform: self.initializeTestValues) // JUST TO LOAD THE TEST DATA
}
}
struct BillView: View {
@Binding var bill: Bill
var body: some View {
VStack() {
ScrollView() {
Group() {
Divider()
HStack() {
Text("Items")
.fontWeight(.semibold)
Spacer()
}
.padding()
ForEach(bill.items, id: \.self) { item in
HStack() {
Text(item.name)
Spacer()
Text("\(item.net, specifier: "%.2f") \(bill.currency.rawValue)")
}
.padding(.horizontal)
}
}
Group() {
Divider()
HStack() {
Text("Total")
.fontWeight(.medium)
Spacer()
Text("\(bill.getTotalNet(), specifier: "%.2f") \(bill.currency.rawValue)")
.fontWeight(.medium)
}
HStack() {
Text("Total with Tax")
.fontWeight(.medium)
Spacer()
Text("\(bill.getTotalWithTax(), specifier: "%.2f") \(bill.currency.rawValue)")
.fontWeight(.medium)
}
Divider()
}
.padding()
Group() {
Text("Choose Tip")
.fontWeight(.bold)
HStack() {
ForEach(Tip.allCases) { tip in
Button(action: {
bill.tip = tip.rawValue
}) {
HStack() {
Text("\(tip.rawValue)")
}
.padding()
.background(bill.tip == tip.rawValue ? Color.indigo : Color.cyan)
.foregroundColor(.white)
.cornerRadius(12)
}
}
}
}
.padding()
}
HStack() {
Text("Amount to pay")
.fontWeight(.bold)
Spacer()
Text("\(bill.getTotalWithTaxAndTip(), specifier: "%.2f") \(bill.currency.rawValue)")
.fontWeight(.bold)
}
}
.padding()
.navigationTitle("Order: \(String(bill.orderNumber))")
}
}
struct Bill: Identifiable, Hashable, Codable {
var id: String = UUID().uuidString
var currency: Currency = .dollar
var taxRate: Double = 7.0
var items: [Item]
var orderNumber: Int
var tip: Int = 10
func getTotalNet() -> Double {
var total: Double = 0.0
for item in items {
total = total item.net
}
return total
}
func getTotalWithTax() -> Double {
return self.getTotalNet() (self.getTotalNet() * taxRate / 100)
}
func getTotalWithTaxAndTip() -> Double {
return self.getTotalWithTax() (self.getTotalNet() * Double(tip) / 100)
}
}
struct Item: Identifiable, Hashable, Codable {
var id: String = UUID().uuidString
var name: String
var net: Double
}
enum Tip: Int, CaseIterable, Identifiable, Codable {
case bad = 0
case good = 10
case better = 20
case best = 30
var id: Self { self }
}
enum Currency: String, CaseIterable, Identifiable, Codable {
case dollar = "$"
case euro = "€"
var id: Self { self }
}
It would look like: