Creating backspace on iOS calculator

2019-04-02 18:05发布

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?

2条回答
男人必须洒脱
2楼-- · 2019-04-02 18:45

Use subStringToIndex method instead of subStringFromIndex

NSString *temp = [string substringToIndex:length-1];
查看更多
手持菜刀,她持情操
3楼-- · 2019-04-02 19:04

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

查看更多
登录 后发表回答