L'observation de valeur key fonctionne-t-elle sur la propriété Text de UITextView?

J'ai le pire moment pour observer la valeur de la key en travaillant avec la propriété text d'UITextView. Je peux append avec succès l'observateur, je peux même enlever ce même observateur. J'ai une vue de table avec plusieurs cellules – certaines ont des UITextFields, d'autres ont des UISegmentSelectors et une a un UITextView. TOUT le rest des champs sont observés avec succès par mon object de données de base (sous-class de NSMangedObject) EXCEPT pour UITextView. Je peux postr du code si nécessaire.

Code Posté:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *UserProfileCellIdentifier = @"UserProfileCellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: UserProfileCellIdentifier]; if (cell == nil) { [_tableCellsNib instantiateWithOwner:self options:nil]; switch (indexPath.row) { // UserName Row case UserNameRowIndex: _textFieldCell.cellLabel.text = NSLocalizedSsortingng(@"UserNameLabel", @"User Profile TableView"); _textFieldCell.cellType = CellTypeNormal; _textFieldCell.boundProperty = @"UserName"; _textFieldCell.tag = indexPath.row; _textFieldCell.errorHandler = self; _textFieldCell.boundControl = _textFieldCell.cellTextField; [_textFieldCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellTextField.text" options:NSKeyValueObservingOptionNew context:NULL]; cell = _textFieldCell; self.textFieldCell = nil; break; case PasswordRowIndex: _textFieldCell.cellLabel.text = NSLocalizedSsortingng(@"PasswordLabel", @"User Profile TableView"); _textFieldCell.cellType = CellTypeNormal; _textFieldCell.cellTextField.secureTextEntry = YES; _textFieldCell.boundProperty = @"Password"; _textFieldCell.tag = indexPath.row; _textFieldCell.errorHandler = self; _textFieldCell.boundControl = _textFieldCell.cellTextField; [_textFieldCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellTextField.text" options:NSKeyValueObservingOptionNew context:NULL]; cell = _textFieldCell; self.textFieldCell = nil; break; case PasswordConfirmRowIndex: _textFieldCell.cellLabel.text = NSLocalizedSsortingng(@"PasswordConfirmLabel", @"User Profile TableView"); _textFieldCell.cellType = CellTypeNormal; _textFieldCell.cellTextField.secureTextEntry = YES; _textFieldCell.tag = indexPath.row; cell = _textFieldCell; self.textFieldCell = nil; break; case FirstNameRowIndex: _textFieldCell.cellLabel.text = NSLocalizedSsortingng(@"FirstNameLabel", @"User Profile TableView"); _textFieldCell.cellType = CellTypeNormal; _textFieldCell.boundProperty = @"FirstName"; _textFieldCell.tag = indexPath.row; _textFieldCell.errorHandler = self; _textFieldCell.boundControl = _textFieldCell.cellTextField; [_textFieldCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellTextField.text" options:NSKeyValueObservingOptionNew context:NULL]; cell = _textFieldCell; self.textFieldCell = nil; break; case LastNameRowIndex: _textFieldCell.cellLabel.text = NSLocalizedSsortingng(@"LastNameLabel", @"User Profile TableView"); _textFieldCell.cellType = CellTypeNormal; _textFieldCell.boundProperty = @"LastName"; _textFieldCell.tag = indexPath.row; _textFieldCell.errorHandler = self; _textFieldCell.boundControl = _textFieldCell.cellTextField; [_textFieldCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellTextField.text" options:NSKeyValueObservingOptionNew context:NULL]; cell = _textFieldCell; self.textFieldCell = nil; break; case DateOfBirthRowIndex: _textFieldCell.cellLabel.text = NSLocalizedSsortingng(@"BirthDateLabel", @"User Profile TableView"); _textFieldCell.cellType = CellTypeDatePicker; _textFieldCell.boundProperty = @"DateOfBirth"; _textFieldCell.tag = indexPath.row; _textFieldCell.errorHandler = self; _textFieldCell.boundControl = _textFieldCell.cellTextField; [_textFieldCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellTextField.text" options:NSKeyValueObservingOptionNew context:NULL]; cell = _textFieldCell; self.textFieldCell = nil; break; case GenderSelfRowIndex: _genderSelectCell.cellLabel.text = NSLocalizedSsortingng(@"UserSexLabel", @"User Profile TableView"); _genderSelectCell.boundProperty = @"GenderSelf"; _genderSelectCell.errorHandler = self; _genderSelectCell.boundControl = _genderSelectCell.cellGenderSegment; _genderSelectCell.tag = indexPath.row; [_genderSelectCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellGenderSegment.selectedSegmentIndex" options:NSKeyValueObservingOptionNew context:NULL]; cell = _genderSelectCell; self.genderSelectCell = nil; break; case GenderInterestdInRowIndex: _genderSelectCell.cellLabel.text = NSLocalizedSsortingng(@"UserInterestdInLabel", @"User Profile TableView"); _genderSelectCell.boundProperty = @"GenderInterestdIn"; _genderSelectCell.errorHandler = self; _genderSelectCell.boundControl = _genderSelectCell.cellGenderSegment; _genderSelectCell.tag = indexPath.row; [_genderSelectCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellGenderSegment.selectedSegmentIndex" options:NSKeyValueObservingOptionNew context:NULL]; cell = _genderSelectCell; self.genderSelectCell = nil; break; case IntroductionRowIndex: _textViewCell.cellLabel.text = NSLocalizedSsortingng(@"IntroductionLabel", @"User Profile TableView"); _textViewCell.boundControl = _textViewCell.cellTextView; _textViewCell.errorHandler = self; _textViewCell.boundProperty = @"Introduction"; _textViewCell.tag = indexPath.row; [_textViewCell addObserver:[MBUtilities getAppDelegate].userProfile forKeyPath:@"cellTextView.text" options:NSKeyValueObservingOptionNew context:NULL]; cell = _textViewCell; self.textViewCell = nil; break; }; } return cell; } 

Discussion: Chaque cellule est chargée depuis un file NIB externe puis initialisée avec des propriétés différentes. Toutes les cellules fonctionnent avec l'observation des valeurs keys sauf pour la dernière avec l'UITextView. Faites-moi savoir si des informations supplémentaires sont nécessaires.

UIKit n'est pas garanti pour être conforme au KVO :

Remarque: Bien que les classs du framework UIKit ne prennent généralement pas en charge KVO, vous pouvez toujours l'implémenter dans les objects personnalisés de votre application, y compris les vues personnalisées.

Cela peut fonctionner sur certaines classs + keys, mais ce n'est pas fiable et cela peut changer entre les différentes versions d'iOS. Voir la réponse de Dave à cette question ; Dave travaille sur UIKit.

L'utilisation de UITextViewTextDidChangeNotification est meilleure.

J'ai fait ce qui suit:

Premier logging avec NSNotificationCenter:

 - (id)init { [super init]; if (self) { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil]; } return self; } - (void)dealloc { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self]; } 

Puis définissez un gestionnaire de notification:

 - (void)textDidChange:(NSNotification *)note { NSLog(@"Observation..."); } 

Maintenant, à chaque fois que le text de l'object UITextView change, la méthode textDidChange: sera appelée.

Ok, donc je n'ai eu aucune chance de find une explication pour ce comportement et à less que quelqu'un puisse me dire le contraire, je crois que c'est un bug dans IOS.

J'ai réussi à le faire fonctionner avec ce hack:

 - (void)textViewDidEndEditing:(UITextView *)textView { NSLog(@"Editing Ended"); textView.text = textView.text; } 

Une fois que ce code a été ajouté, voila, le widget observeValueForKeyPath se triggers!

Avez-vous essayé d'implémenter UITextViewDelegate et lui affecter le thisIsYourTextView.delegate?

Eh bien, j'ai trouvé ce tutoriel , peut-être que ça va rendre les choses un peu plus claires.