如何在iOS中为自定义键盘创建删除按钮?

时间:2013-11-16 06:11:36

标签: 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
}
}

3 个答案:

答案 0 :(得分:3)

您可以使用此

-(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)];

答案 1 :(得分:1)

您的代码在许多不同的情况下都不起作用 - 例如,如果用户将插入点移动到文本的中间,该怎么办?如果他们选择了一系列文字并打了一个数字怎么办?

幸运的是,UITextView现在支持UITextInput协议。这使得在所有情况下都可以轻松支持自定义键盘并适当更新文本。作为额外的奖励,它支持deleteBackwards,这使得支持自定义键盘上的删除功能变得微不足道。

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

    NSString *numString = sender.titleLabel.text;
    UITextRange *selectedText = [display selectedTextRange];
    if (!selectedText)
    {
        // no selected text or insertion point
    }
    else if (selectedText.empty)
    {
        // inserting number at an insertion point
        [display replaceRange:selectedText withText:numString];
    }
    else
    {
        // update selected range with number
        [display replaceRange:selectedText withText:numString];
    }
}


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

    UITextRange *selectedText = [self.delegate selectedTextRange];
    if (!selectedText)
    {
        // no selected text or insertion point
    }
    else if (selectedText.empty)
    {
        // deleting text at an insertion point
        [display deleteBackward];
    }
    else
    {
        // update selected range with empty string
        [display deleteBackward];
    }
}

答案 2 :(得分:0)

要做到这一点,你需要添加一些代码。见下文。

-(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 setText:newText];  //set the display equal to that string
    } 
    else 
    {
        if([digit isEqualToString:@"delete"]) //identify that your delete button pressed
        {
            NSString *newTextAfterDelete=[[display text] deleteCharactersInRange:NSMakeRange([[display text] length]-1, 1)];
            [display setText:newTextAfterDelete];      
        }
        else
        {
            NSString *newText = [NSString stringWithFormat:@"%@%@", [display text], digit];      //add the title of the pressed button to the string we already have
            [display setText:newText]; //set it as the display text
        }
    }
}