2016-03-30 40 views

回答

1

應實現協議UITextViewDelegate方法

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; 

,只是得到範圍 試試這個有一個日誌文本(這是在的情況下空它不是一個替代而是一種新的輸入)

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 


     NSLog(@"deleting this string: |%@|", [textView.text substringWithRange:range]); 


    return YES; 
} 

PS

記得設置你的類作爲您的UITextView的代表

+0

對不起,我不明白我只是想檢測哪些字符在鍵盤背部空間刪除... '文字'返回新的替換字符右未刪除字符.. –

+0

好吧,對不起...我改變了我的答案... – meronix

+0

哎那真棒人爲我工作感謝您的幫助.. –

-1

這就是你想要的。該方法檢測到一個字符被刪除,並將其打印到控制檯。

@interface ViewController()<UITextFieldDelegate> 
{ 
    UITextField *textField; 
    NSString *currentText; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 30)]; 
    [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged]; 
    textField.delegate = self; 
    textField.borderStyle = UITextBorderStyleRoundedRect; 
    [self.view addSubview:textField]; 
} 

-(void)textChanged:(UITextField *)sender{ 

    if (!currentText) { 
     currentText = sender.text; 
    } 

    if (![currentText isEqualToString:sender.text]) { 

     //The text that is in the textField at the moment is shorter than it was the last time the textfield was editted... This shows that a backspace was pressed 
     if (currentText.length > sender.text.length) { 


      NSLog(@"Character: %@", [currentText substringFromIndex:sender.text.length]); 

     } 



    } 

    currentText = sender.text; 


} 


@end