Search code examples
iphoneiosuikeyboarduikeyinput

how to show different letters in textfield when clicking different buttons of keyboard in iOS


I am doing a keyboard by calling UIView and I have many buttons with different letters. I don't know how to get different letters in textfield because when I touch different button, it show the same letter in textfield. what is the code for telling that user touches the different button ?


Solution

  • All of the buttons should call the same IBAction.

    Mine is called "-(IBAction)addCharacter:(id)sender".

    Then you just do something like this:

    -(IBAction)addCharacter:(id)sender
    {
        //you can send an UIButton directly too.
        UIButton *charBtn = (UIButton *)sender;
        NSString *buttonPressed = charBtn.currentTitle.
        //Your UITextField gets modified here.
        //This method will append whatever button is pressed at the end of the string.
        someTextField.text = [someTextField.text stringByAppendingString:buttonPressed];
    }
    

    That's the basics. Obviously if your keyboard needs to remove the last character, you will need to modify, but it should be easy to do using this base code.