user830859
user830859

Reputation: 1

Creating backspace on iOS calculator

I'm trying to add a backspace button to remove the last entered digit in a calculator, but I can't seem to get it. Here is what I've tried:

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

    NSMutableString *string = (NSMutableString*)[display text];

    int length = [string length];

    NSString *temp = [string substringFromIndex:length-1];

    [display setText:[NSString stringWithFormat:@"%@",temp]];

}

Any ideas?

Upvotes: 0

Views: 1356

Answers (2)

KingofBliss
KingofBliss

Reputation: 15115

Use subStringToIndex method instead of subStringFromIndex

NSString *temp = [string substringToIndex:length-1];

Upvotes: 1

Dr. Acula
Dr. Acula

Reputation: 2422

I think you want [string substringToIndex:length-1] instead of [string substringFromIndex:length-1].

Upvotes: 3

Related Questions