Alex Moore
Alex Moore

Reputation: 11

How do I create a simple backspace button on iOS?

I have a custom numeric keypad on which I want to include a special "clear" button—more or less a backspace button that removes the last character. I can't find any way to get this button to work. Here's the simple code:

-(IBAction)buttonDigitPressed:(id)sender {
    currentNumber = currentNumber*10 + (double)[sender tag];
    priceOne.text = [NSString stringWithFormat:@"%.2f",currentNumber / 100.0f];
}

-(IBAction)cancelOperation {
    (backspace code goes here)
}

Any ideas would be great! Thanks!

Upvotes: 0

Views: 1367

Answers (2)

Tamás
Tamás

Reputation: 48071

Representing the number as a float or double might be a bad idea; you might run into trouble with floating point roundoff errors when the number you typed in the text field cannot be represented exactly as a floating point number. (This happens with fractional numbers only of course). Why don't you store the number as an NSString? You can then simply chop off the last digit using string operations and convert it to a number using [NSString doubleValue] when you need the numeric value.


Edit: okay, so here's a bit more. Let us assume that currentNumber is an NSMutableString* instead of a double. I'm writing the following code off the top of my head, but it shows the general idea:

- (IBAction)buttonDigitPressed:(id)sender {
    [currentNumber appendString:[NSString stringFromFormat:@"%d", [sender tag]]];
    priceOne.text = currentNumber;
}

- (IBAction)cancelOperation {
    [currentNumber deleteCharactersInRange:NSMakeRange([currentNumber length]-1, 1)];
    priceOne.text = currentNumber;
}

Whenever you need the actual value in the text box, you just simply say [currentNumber doubleValue]. In fact, you don't even need currentNumber as you can start manipulating priceOne.text directly.

Upvotes: 1

David Schaefgen
David Schaefgen

Reputation: 800

Ok, I'm going to try and interpret your sample a bit and walk you through what I would do to get a behavior like the standard keyboard backspace button. If you want to leave comments to correct my assumptions, I'll update accordingly.

The first thing you would want to do is track the currently active UITextField in your view controller or the class for your keypad. The only real option I can see would be to delete backward from the end as there is no non-private way to get the current cursor location in a UITextField. Assuming priceOne is your text field something like:

priceOne.text = [priceOne.text substringToIndex:priceOne.text.length - 1];

Now, this should behave fine as long as you have text in the text field but you'll have issues if priceOne has no text in it so check that it's length is greater than 0.

To really get the behavior of the standard keyboard, I would suggest considering a move away from UIButton so that you can enable your control to fire repeatedly via an NSTimer during a long press and other subtle behaviors.

PS| I agree with Tamás that it is likely a poor idea to use float or double to represent currency values; particularly so if you intend to do math with them.

Upvotes: 0

Related Questions