iphone - 键值观察对 UITextView 的 Text 属性有效吗?

标签 iphone objective-c ios

我在使用 UITextView 的文本属性进行键值观察时遇到了最糟糕的情况。我可以成功添加观察者,甚至可以删除同一个观察者。我有一个包含多个单元格的表格 View - 一些具有 UITextFields,一些具有 UISegmentSelectors,一个具有 UITextView。除了 UITextView,我的核心数据对象(NSMangedObject 的子类)成功观察了所有其余字段。如果需要,我可以发布代码。

发布代码:

- (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 的单元格外,所有单元格都使用键值观察。如果需要任何其他信息,请告诉我。

最佳答案

UIKit 不保证是 KVO compliant :

Note: Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views.

它可能适用于某些类 + 键,但这并不可靠,并且可能会在不同的 iOS 版本中发生变化。参见 Dave’s answer to this question ; Dave 致力于 UIKit。

关于iphone - 键值观察对 UITextView 的 Text 属性有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6333600/

相关文章:

ios - 为什么 UITableViewCell 保持高亮?

iphone - 如何从 Xcode 通过短信发送音乐文件

iphone - 将对象传递给 NSThread 选择器

iphone - 我如何将 NSData 转换为 NSArray?

iphone - 对于 "as-you-type"搜索,Core Data 是否比自定义索引更有效?

objective-c - 为什么这种使用 CATransform3D 的转换会减小 View 的宽度?

ios - 将 UIView 移动到 UITableView 之上 - 触摸顶部 UIView 仍会选择表格行

ios - 曼哈顿启发式 IDA* 算法的正确实现是什么?

ios - 符合协议(protocol)的Swift类,如何将其作为参数传递?

iphone - 在PCH文件中定义一个宏,给出警告