我有最糟糕的時間獲得關鍵值觀察工作與UITextView的文本屬性。我可以成功添加觀察者,我甚至可以刪除同一個觀察者。我有一個tableview與幾個單元格 - 一些有UITextFields,一些有UISegmentSelectors和一個有一個UITextView。我的核心數據對象(NSMangedObject的子類)成功觀察了其餘所有字段,除了UITextView以外。如果需要,我可以發佈代碼。觀察UITextView文本屬性的關鍵值?
發佈代碼:
- (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的最後一個外,所有單元格都可以使用鍵值觀察。讓我知道是否需要任何額外的信息。
我該如何在自定義視圖中實現這一點?我會重寫什麼方法來改變這種行爲? – jjm340 2011-06-15 16:52:40
@jjm你只能實現這個自定義鍵 - 例如,如果你聲明一個新的屬性。如果您有從UIKit類繼承的自定義視圖,則KVO不一定適用於該UIKit類中已存在的鍵。 – 2011-06-15 21:49:37