我在 Xcode 中制作了一个自定义键盘。它具有普通四功能计算器的所有按键。 目前,这是我用来制作计算器类型的方法。它获取被按下按钮的标题并将其添加到字符串 display.text 的末尾。 这是我用来让键盘向显示器添加数字的代码。如何从显示屏上删除数字?我可以添加此代码还是需要完全更改它?
-(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
}
}
最佳答案
你可以用这个
-(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];
}
或者你也可以用它来删除最后一个字符
[myString deleteCharactersInRange:NSMakeRange([myString length]-1, 1)];
关于ios - 如何在 iOS 中为自定义键盘制作删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015396/