user1676682
user1676682

Reputation: 381

How do I make a delete button for a custom keypad in iOS?

I've made a custom keypad in Xcode. It has all the keys of a regular four-function calculator. Currently, this is the method I'm using to make the calculator type. It takes the title of the button being pressed and essentially adds it to the end of the string, display.text. This is the code I'm using to make the keyboard add a digit to the display. How do I delete a digit from the display? Can I add to this code or do I need to change it entirely?

-(IBAction)digitPressed:(UIButton *)sender{
    NSString *digit = [sender currentTitle]; //take the title of the button being pressed

if (display.text==nil){ //if there's no text in the display
    NSString *newText = [NSString stringWithFormat:@"%@", digit]; //create a string out of the title of the button being pressed
    display.text = newText; //set the display equal to that string
} else {
    NSString *newText = [NSString stringWithFormat:@"%@%@", display.text, digit]; //add the title of the pressed button to the string we already have
    display.text = newText; //set it as the display text
}
}

Upvotes: 0

Views: 596

Answers (3)

memmons
memmons

Reputation: 40502

Your code won't work in a number of different cases -- for example, what if the user moves the insertion point to the middle of the text? What if they select a range of text and hit a number?

Luckily, UITextView now supports UITextInput protocol. This makes it easy to support custom keyboards and update the text appropriately in all cases. As an added bonus, it supports deleteBackwards which makes supporting delete functionality on custom keyboards trivial.

- (IBAction)numButtonAction:(UIButton *)sender {

    NSString *numString = sender.titleLabel.text;
    UITextRange *selectedText = [display selectedTextRange];
    if (!selectedText)
    {
        // no selected text or insertion point
    }
    else if (selectedText.empty)
    {
        // inserting number at an insertion point
        [display replaceRange:selectedText withText:numString];
    }
    else
    {
        // update selected range with number
        [display replaceRange:selectedText withText:numString];
    }
}


- (IBAction)deleteButtonAction:(UIButton *)sender {

    UITextRange *selectedText = [self.delegate selectedTextRange];
    if (!selectedText)
    {
        // no selected text or insertion point
    }
    else if (selectedText.empty)
    {
        // deleting text at an insertion point
        [display deleteBackward];
    }
    else
    {
        // update selected range with empty string
        [display deleteBackward];
    }
}

Upvotes: 1

Tapas Pal
Tapas Pal

Reputation: 7207

To do so just you need to add some line of code. see below.

-(IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [sender currentTitle]; //take the title of the button being pressed

    if ([display text]==nil) //if there's no text in the display
    { 
        NSString *newText = [NSString stringWithFormat:@"%@", digit]; //create a string out of the title of the button being pressed
        [display setText:newText];  //set the display equal to that string
    } 
    else 
    {
        if([digit isEqualToString:@"delete"]) //identify that your delete button pressed
        {
            NSString *newTextAfterDelete=[[display text] deleteCharactersInRange:NSMakeRange([[display text] length]-1, 1)];
            [display setText:newTextAfterDelete];      
        }
        else
        {
            NSString *newText = [NSString stringWithFormat:@"%@%@", [display text], digit];      //add the title of the pressed button to the string we already have
            [display setText:newText]; //set it as the display text
        }
    }
}

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31091

You can use this

-(IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [sender currentTitle];
    display.text=[display.text stringByAppendingString:digit];
}

-(IBAction)deletePressed:(UIButton *)sender
{
    if ( [display.text length] > 0)
        display.text = [display.text substringToIndex:[display.text length] - 1];
}

Or you can also use this to delete last char

[myString deleteCharactersInRange:NSMakeRange([myString length]-1, 1)];

Upvotes: 3

Related Questions