iOSBeginner
iOSBeginner

Reputation: 103

delete last character of string with self-made keyboard

i have a uitextfield (_nameField) and buttons a-z, space, minus, delete and ok.

the buttons a-z, space, minus have the following same ibaction:

NSString *text1 = @"";

- (IBAction)keyboardButtons:(id)sender {


if (_nameField.text.length <=14) {

    if (_nameField.isEditing) {
        text1 = [text1 stringByAppendingString:[sender currentTitle]];
        [_nameField setText:text1];
    }
}

}

works perfectly.

the delete button have this ibaction:

- (IBAction)clearButton:(id)sender {

if (_nameField.isEditing) {
    text1 = @"";
    [_nameField setText:text1];
}


}

ok, thats pretty simple, it sets the text of my string to @"". but i want to delete just the last character. how can i do that?

Upvotes: 0

Views: 68

Answers (2)

vadian
vadian

Reputation: 285082

just write

text1 = [text1 substringToIndex:(text1.length -1)]

Upvotes: 2

mszaro
mszaro

Reputation: 1372

Obj-C doesn't provide a method to remove the last character, but you can achieve the same thing by taking a substring that includes all characters except the last one.

NSString* withoutLastChar = [originalString substringToIndex: [originalString length] - 1];

Upvotes: 0

Related Questions