从字符串Xcode中删除一个字符

时间:2014-11-06 02:22:44

标签: xcode6 nsrange custom-keyboard

我使用以下代码将字符添加到可变字符串,然后将其从自定义键盘放入textview

- (IBAction) press:(id)sender {
    [[UIDevice currentDevice] playInputClick];
    if (sender == AEE) {
        self.BACK.hidden = NO;
        A = @"A";
        NSLog(@"sender:%@",sender);
    }
    if (sender == BEE) {
        self.BACK.hidden = NO;
        A = @"B";
    }

    if (sender == CEE) {
        self.BACK.hidden = NO;
        A = @"C";
    }

    if (sender == DEE) {
        self.BACK.hidden = NO;
        A = @"D";
    }

    if (sender == EEE) {
        self.BACK.hidden = NO;
        A = @"E";
    }

    if (sender == EFF) {
        self.BACK.hidden = NO;
        A = @"F";
    }

    if (sender == ONE) {
        self.BACK.hidden = NO;
        A = @"1";
    }

    if (sender == TWO) {
        self.BACK.hidden = NO;
        A = @"2";
    }

    if (sender == THREE) {
        self.BACK.hidden = NO;
        A = @"3";
    }

    if (sender == FOUR) {
        self.BACK.hidden = NO;
        A = @"4";
    }

    if (sender == FIVE) {
        self.BACK.hidden = NO;
        A = @"5";
    }

    if (sender == SIX) {
        self.BACK.hidden = NO;
        A = @"6";
    }

    if (sender == SEVEN) {
        self.BACK.hidden = NO;
        A = @"7";
    }

    if (sender == EIGHT) {
        self.BACK.hidden = NO;
        A = @"8";
    }

    if (sender == NINE) {
        self.BACK.hidden = NO;
        A = @"9";
    }

    if (sender == ZERO) {
        self.BACK.hidden = NO;
        A = @"0";
    }




    if (display.length <= 7) {
        [self.display appendString:A];
         DONE.hidden = YES;
    }
    if (display.length == 8) {
        DONE.hidden = NO;
    }


    else {
        [self.display appendString:@""];
    }


    //**THIS IS WHERE I DELETE CHARATERS**
    if (sender == BACK) {


                [display deleteCharactersInRange:NSMakeRange([display length]-2, 2)];
                       NSLog(@"display2:%@", display);
        if (self.display.length <1) {
            BACK.hidden = YES;
        }
        if (display.length < 8) {
            DONE.hidden = YES;
        }
    }


    you.text = display;
}

当我使用代码删除字符时,它适用于所有字符1 - 7.无论我在哪里,如果我按下退格键,它会删除一个字符。但是,当我按下退格按钮删除一个包含8个字符的字符时,它会删除两个字符。如果我将代码更改为仅删除一个字符,如

[display deleteCharactersInRange:NSMakeRange([display length]-1, 1)];

它只删除一个字符一次。如果我把

[display deleteCharactersInRange:NSMakeRange([display length]-1, 2)];

它做同样的事情。但是当我把

[display deleteCharactersInRange:NSMakeRange([display length]-2, 1)];

我的应用程序崩溃,我得到一个越界错误。我试过使用如果display =&lt; 7使用一个-2,2代码,如果显示== 8使用-1,1代码,但没有任何东西使它一直减少一个字符的数量。有没有人有任何建议?

1 个答案:

答案 0 :(得分:0)

我想通了如果我创建了一个单独的动作来删除并使用下面的代码,一切似乎都运行正常

  • (IBAction)删除:(id)发件人{ [[UIDevice currentDevice] playInputClick]; if(sender == BACK){     if([display length]&gt; 0){         [display setString:[display substringToIndex:[display length] -1]];         you.text = display;

    }
    else {
        you.text = display;
        //no characters to delete... attempting to do so will result in a crash
    }
    if (self.display.length <1) {
        BACK.hidden = YES;
    }
    if (display.length < 8) {
        DONE.hidden = YES;
    }
    

    }