1

custom keyboard for tip calculator Practicing swift making a tip calculator. I know using a number keyboard would make this a lot easier but I'm wanting this custom "keyboard" or number pad like a calculator app to be able to edit the text field "TotalBill". I know you can do this by updating a label with the button numbers being pressed but there is no user notification that something will happen if you start typing on the buttons.

I hope that all made sense. Thanks.

Updated:

var userTyping = false
@IBOutlet weak var totalBill: UITextField!
@IBAction func numberDelete(_ sender: UIButton) {
    if let currentTitle = perPersonTotal.text{
    let updatedTitle = currentTitle.substring(to: (currentTitle.index(before: (currentTitle.endIndex))))
    perPersonTotal.text = updatedTitle
    }
}
@IBOutlet weak var perPersonTotal: UILabel!
@IBAction func touchDigit(_ sender: UIButton) {
    if let digit = sender.currentTitle {
        if userTyping {
            let currentDisplay = perPersonTotal.text!
            perPersonTotal.text = currentDisplay + digit
        } else {
            perPersonTotal.text = digit
        }
        userTyping = true
    }

}
DevNinja
  • 60
  • 9
  • That looks like a custom view built with buttons in 3x5 layout. Please show what you have tried. – Teja Nandamuri Aug 29 '16 at 16:41
  • 3x5 stackviews of buttons.. yes.. I have updated my post to what I have tried. – DevNinja Aug 29 '16 at 16:59
  • IF you are targeting iOS 9 + you can use stack views. You can get the value of pressed button in many ways. Please show the code of what you have tried. – Teja Nandamuri Aug 29 '16 at 17:01
  • You can use the tag value of button, of you can use the button text, and convert it to a number, or you can even write a method that returns a number when the buttonis clicked. – Teja Nandamuri Aug 29 '16 at 17:03
  • 1
    can you check this http://stackoverflow.com/questions/33474771/a-swift-example-of-custom-views-for-data-input-custom-in-app-keyboard – Nazmul Hasan Aug 29 '16 at 17:40
  • Since I don't have enough reputation to comment on that post Nazmul, I got it to work perfectly with the custom keyboard but is there a way to do it without adding it as a custom keyboard... like using my current stack view as the keyboard? with the stack buttons not there it is messing with my layout for different device sizes... – DevNinja Aug 29 '16 at 19:43

0 Answers0