问题描述
我遇到了使用UITextView的text属性进行键值观察的最糟糕时间。我可以成功添加观察者,我甚至可以删除同一个观察者。我有一个带有几个单元格的tableview - 有些有UITextFields,有些有UISegmentSelectors,有一个有UITextView。对于UITextView,我的核心数据对象(NSMangedObject的子类)EXCEPT成功观察到所有其余字段。如果需要,我可以发布代码。
I'm having the worst time getting key value observing working in with a UITextView's text property. I can successfully add the observer, I can even remove that same observer. I have a tableview with several cells - some have UITextFields, some have UISegmentSelectors and one has a UITextView. ALL the rest of the fields are successfully observed by my core data object (subclass of NSMangedObject) EXCEPT for the UITextView. I can post code if needed.
已发布代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *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 = NSLocalizedString(@"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 = NSLocalizedString(@"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 = NSLocalizedString(@"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 = NSLocalizedString(@"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 = NSLocalizedString(@"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 = NSLocalizedString(@"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 = NSLocalizedString(@"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 GenderInterestedInRowIndex:
_genderSelectCell.cellLabel.text = NSLocalizedString(@"UserInterestedInLabel", @"User Profile TableView");
_genderSelectCell.boundProperty = @"GenderInterestedIn";
_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 = NSLocalizedString(@"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;
}
讨论:从外部NIB文件加载每个单元格,然后使用不同的属性进行初始化。所有单元格都使用键值观察,除了最后一个使用UITextView。如果需要任何其他信息,请告诉我。
Discussion: Each cell is loaded from an external NIB file then initialized with different properties. All of the cells work with key value observing except for the very last one with the UITextView. Let me know if any additional info is needed.
推荐答案
UIKit不保证:
它可能适用于某些类+键但这不可靠,并且可能会在不同的iOS版本中发生变化。请参阅; Dave在UIKit工作。
It might work on some classes + keys but that’s not reliable, and might change across different iOS versions. See Dave’s answer to this question; Dave works on UIKit.
这篇关于密钥值观察是否适用于UITextView的文本属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!