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

decimalPad and custom button for minus sign

Forums > SwiftUI

I'm trying to add a custom button to the decimalPad so that I can input negative integers for various fields, but I'm stuck. I have no idea what to put in the Button code. I've seen other apps do this and I tried searching on the Internet but couldn't really find anything relavent. I know a workaround is to use the numbersAndPunctuation keyboard but I'd prefer the decimalPad. Can someone please help?

struct ContentView: View {
    @State private var amount1 = 0
    @State private var amount2 = 0
    @State private var amount3 = 0
    @FocusState var amountIsFocused: Bool

    var body: some View {
        NavigationView{
            Form {
                TextField("Amount 1", value: $amount1, format: .number)
                    .focused($amountIsFocused)
                    .keyboardType(.decimalPad)
                TextField("Amount 2", value: $amount2, format: .number)
                    .focused($amountIsFocused)
                    .keyboardType(.decimalPad)
                TextField("Amount 3", value: $amount3, format: .number)
                    .focused($amountIsFocused)
                    .keyboardType(.decimalPad)
            }
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("-") {

                    }
                    Button("Done") {
                        amountIsFocused = false
                    }
                }
            }
        }
    }
}

2      

I played around with this some more and still haven't figured out how to implement it on the keyboard. This is a step in the direction, I'm trying to go, though it seems like duct tape coding. There's got to be a better way to do this.

enum WhichAmount {
    case one, two, three
}

class Amounts: ObservableObject {
    @Published var amount1 = 0
    @Published var amount2 = 0
    @Published var amount3 = 0
}

struct MinusView: View {
    @ObservedObject var amounts: Amounts
    let whichAmount: WhichAmount
    @FocusState var amountIsFocused: Bool

    var body: some View {
        HStack {
            TextField("Enter value:", value: whichAmount == .one ? $amounts.amount1 : whichAmount == .two ? $amounts.amount2 : $amounts.amount3, format: .number)
                .keyboardType(.decimalPad)
                .focused($amountIsFocused)
            Button("-") {
                switch whichAmount {
                case .one:
                    amountIsFocused = false
                    amounts.amount1 *= -1
                case .two:
                    amountIsFocused = false
                    amounts.amount2 *= -1
                case .three:
                    amountIsFocused = false
                    amounts.amount3 *= -1
                }
            }
        }
    }
}

struct ContentView: View {
    @StateObject var amounts = Amounts()
    @FocusState var amountIsFocused: Bool

    var body: some View {
        NavigationView{
            Form {
                MinusView(amounts: amounts, whichAmount: .one)
                MinusView(amounts: amounts, whichAmount: .two)
                MinusView(amounts: amounts, whichAmount: .three)
            }
        }
    }
}

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.