1

I have a static numeric-keyboard made out of a bunch of buttons, I also have three UITextFields, textField1, textField2 and textField3 where I'm inputting the text using the static keyboard.

Here is the code I'm using to detect which textField is currently in focus and to input the content of the buttons. It kind of works but I don't like the fact that I have three IF statements and I'm not sure how to prevent the keyboard from appearing when a textField is tapped.

What would be the best way to implement this functionality?

@IBAction func appendKey(sender: AnyObject) {
    let digit = sender.currentTitle!

    if(textField1.isFirstResponder()){
        textField1.text = textField1.text! +  digit!
    }else if(textField2.isFirstResponder()){
        textField2.text = textField2.text! +  digit!
    }else if(textField3.isFirstResponder()){
        textField3.text = textField3.text! +  digit!
    }
}

Thanks

fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 1
    Code reviews should be posted at http://codereview.stackexchange.com. – rmaddy Aug 24 '16 at 01:13
  • This is really not a code review, I need to know the proper way to implement such of functionality, in fact now I'm not even sure how to prevent the default keyboard from appearing. – fs_tigre Aug 24 '16 at 01:16
  • You state *"Everything works fine but I don't like the fact that I have three IF statements, how can this code be improved?"* - that's a request for a code review. – rmaddy Aug 24 '16 at 01:17
  • I know, I apologized. I just edit my question to make it more clear that I still need help implementing this functionality. – fs_tigre Aug 24 '16 at 01:20
  • If the standard keyboard is displaying then how is your custom keyboard setup? Your custom keyboard should be the `inputView` of each `UITextField`. If you do that, the standard keyboard won't appear and yours will instead. – rmaddy Aug 24 '16 at 01:24
  • I tried attaching all `UITextFields` to a `TouchDown` event to `resignFirstResponder()` there but it didn't work. – fs_tigre Aug 24 '16 at 01:28
  • The custom keyboard are now just a bunch of buttons sitting on the top of the screen and the standard keyboard show underneath, I know, crazy. Right now I'm just trying different things. – fs_tigre Aug 24 '16 at 01:30

1 Answers1

3

If the standard keyboard is displaying then your custom keyboard isn't setup properly. Your custom keyboard should be the inputView of each UITextField. If you do that, the standard keyboard won't appear and yours will instead.

Your custom keyboard should be a separate class that handles all of it's own buttons. It appears you have everything in one view controller - all of the text fields, all of the buttons, and all of the button handling code. This is a bad approach. Create your custom keyboard class view. Put all of the code to handle and display the buttons in that custom view class. Create a single instance of this view in your view controller and assign the custom keyboard view instance to the inputView property of each text field.

In the custom keyboard class, listen for the UITextFieldTextDidBeginEditingNotification notification. This is how you keep track of the current text field. Your custom keyboard class should not have any specific reference to any text field other than track the current one. It should also ensure that the text field's inputView is itself.

In each button handler of the custom keyboard class, get the text you wish to append and then call the text field's insertText: method with the string. That's it. This will ensure the text is inserted and/or replaced based on the current selecting in the text field.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You are absolutely right, I have everything within the same view. Your approach seems reasonable unfortunately I think it is a little more complicated than what my knowlege can handle. Have you seen any tutorial or something that I can reference as a starting point? I will mark your answer as the correct answer because you explained the process, I guess is my problem for not understanding it :(. – fs_tigre Aug 24 '16 at 01:39
  • See http://stackoverflow.com/questions/13205160/how-do-i-retrieve-keystrokes-from-a-custom-keyboard-on-an-ios-app but it's all in Objective-C. Not sure about anything specific to Swift. – rmaddy Aug 24 '16 at 01:43
  • I will try to digest the Objective-C code and try to convert it to Swift. Thanks a lot for your help. – fs_tigre Aug 24 '16 at 01:48