ios - 如何在 iOS 中为自定义键盘制作删除按钮?

标签 ios iphone objective-c ios7

我在 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/

相关文章:

iPhone MapKit 是否可以从一系列触摸中映射坐标?

iphone - 如何检查NSTimer是否已经自动失效?

iphone - 将 NSString 拆分为 UITextField 页面

objective-c - 使用属性字符串突出显示 NSStatusItem

ios - 条件声明 Swift 上的数组索引超出范围错误

ios - 如何使用选项字典关闭 Swift 中的核心数据预写日志记录?

ios - 与照片相关的警报不会调用 addUIInterruptionMonitor 的处理程序

iphone - JEFF LAMARCHE 带提示的警报 View 在 iOS 4 中关闭屏幕

objective-c - 如何将 UISerachBar 添加到 UIToolBar?

ios - 如何在 iOS 中创建带有阴影和 subview 的 UIimageView