Creating backspace on iOS calculator

二次信任 提交于 2019-12-05 08:00:30

问题


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?


回答1:


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




回答2:


Use subStringToIndex method instead of subStringFromIndex

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


来源:https://stackoverflow.com/questions/6591551/creating-backspace-on-ios-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!