UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: day 18 - picker to be a text field

Forums > 100 Days of SwiftUI

Good afternoon everyone. Please explain to me how Change the “Number of people” picker to be a text field, making sure to use the correct keyboard type. This is my first programming language, and i don't understand how to do task №3.



```struct ContentView: View {

    init() {
        UITableView.appearance().backgroundColor = color
    }

    @State private var checkAmount = ""
    @State private var numberOfPeople = 2
    @State private var tipPercentage = 2

        var totalPerPerson: Double {
        let peopleCount = Double(numberOfPeople + 2)
        let tipSelection = Double(tipPercentages[tipPercentage])
        let orderAmount = Double(checkAmount) ?? 0

        let tipValue = orderAmount / 100 * tipSelection
        let grandTotal = orderAmount + tipValue
        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }

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

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Amount", text: $checkAmount)
                        .keyboardType(.decimalPad)

                        Picker("Number of people", selection: $numberOfPeople) {
                            ForEach(2 ..< 100) {
                                Text("\($0) people")

                    }
                }
            }
                Section(header: Text("How much tip do you want to leave?")) {
                    Picker("Tip percentage", selection: $tipPercentage) {
                        ForEach(0 ..< tipPercentages.count) {
                            Text("\(self.tipPercentages[$0])%")
                    }
                }
            }
                    .pickerStyle(SegmentedPickerStyle())

                Section(header: Text("Amount per person")) {
                    Text("$\(totalPerPerson, specifier: "%.2f")")
                    }
        Section(header: Text("Total Amount for the check")) {
            Text("$\((Double((numberOfPeople) + 2) * totalPerPerson), specifier: "%.2f")")
                }
            }
            .navigationBarTitle("WeSplit")
        }
    }
}

2      

Change to this

struct ContentView: View {
    @State private var checkAmount = ""
    @State private var numberOfPeople = ""
    @State private var tipPercentage = 2

    // ADD COMPUTED PROPERTY TO TURN FROM STRING TO DOUBLE (DEFAULT ONE IF NOT A NUMBER ENTERED)
    var peopleCount: Double {
        (Double(numberOfPeople) ?? 1)
    }

    var totalPerPerson: Double {
//        let peopleCount = Double(numberOfPeople + 2) <- REMOVE THIS
        let tipSelection = Double(tipPercentages[tipPercentage])
        let orderAmount = Double(checkAmount) ?? 0

        let tipValue = orderAmount / 100 * tipSelection
        let grandTotal = orderAmount + tipValue
        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }

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

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Amount", text: $checkAmount)
                        .keyboardType(.decimalPad)

                    // CHANGE TO TEXTFIELD WITH NUMBER PAD KEYBOARD
                    TextField("Number of people", text: $numberOfPeople)
                        .keyboardType(.numberPad)

//                    Picker("Number of people", selection: $numberOfPeople) {
//                        ForEach(2 ..< 100) {
//                            Text("\($0) people")
//
//                        }
//                    }
                }

                Section(header: Text("How much tip do you want to leave?")) {
                    Picker("Tip percentage", selection: $tipPercentage) {
                        ForEach(0 ..< tipPercentages.count) {
                            Text("\(self.tipPercentages[$0])%")
                        }
                    }
                }
                .pickerStyle(SegmentedPickerStyle())

                Section(header: Text("Amount per person")) {
                    Text("$\(totalPerPerson, specifier: "%.2f")")
                }

                Section(header: Text("Total Amount for the check")) {
                    Text("$\((peopleCount * totalPerPerson), specifier: "%.2f")") // <- CHANGE TO `peopleCount`
                }
            }
            .navigationBarTitle("WeSplit")
        }
    }
}

4      

@NigelGee - thank you very much)

2      

Thanks!!!!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.