2013-07-10 63 views
0

這件事讓我瘋狂。任何人如何響應用戶在數字鍵盤上按下DELETE鍵?我無法找到它的委託方法,並且只有在調用shouldChangeCharactersInRange()時纔會看到一個空字符串。響應DELETE on UIKeyboardTypeDecimalPad

謝謝!

+0

檢查實際範圍值。如果字符範圍小於可能表示刪除的字符串的長度。 – Bergasms

回答

0

我想通了......

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
               replacementString:(NSString *)string { 

    if ([string length] == 0) 
     return YES; 

    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; 
    string = [[string componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:@""]; 
    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; 


return NO; 

}