最近在做一個需求的時候,出現了這樣一個問題:textViewDidChange當程序中給UITextView賦值的時候,是不被調用的,如官方文檔對該方法的說明:
Tells the delegate that the text or attributes in the specified text view were changed by the user.
The text view calls this method in response to user-initiated changes to the text. This method is not called in response to programmatically initiated changes.
如果必要在程序中給UITextView賦值又希望有相應的動作的話,我采用了一個替代方案:
1.在合適的地方注冊一個系統的通知(注意name,系統定義的):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChangedExt:) name:UITextViewTextDidChangeNotification object:nil];
2.實現textChangedExt:
- (void)textChangedExt:(NSNotification *)notification
{
//todo sth...
}