处理UITextField时如何避免编译器警告与UITextView相同

时间:2011-07-09 22:17:33

标签: objective-c protocols compiler-warnings

情景:

我有两个UITableViewCells,分别包含UITextField属性和UITextView属性。显然,这两个属性都有共同的属性,例如'text'和'textColor'

目标:

不加区别地将字符串分配给UITextField / UITextView的文本属性,而不会引发编译器警告。可以吗?

以下是我正在做的一个例子:

目前我这样做是为了避免编译器警告:

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UITextField *textField;
UITextView *textView;
switch (indexPath.section) {
    case SectionDescription:
        textField = (UITextField *)[(TextFieldCell *)cell contents];
        textField.text = self.plan.description;
        break;

    case SectionNotes:
        textView = (UITextView *)[(TextViewCell *)cell contents];
        textView.text = self.plan.notes;
        break;
}

但是我想在没有编译器警告的情况下更接近这一点:

NSString *contents = [self getContents];

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIView *textView = (?? *)[(?? *)cell contents];
textView.text = contents;

有没有标准的方法来做到这一点?我知道如果两个对象都符合通用协议,我可以做一些像“UIView * textView = [cell contents];”这个问题有答案吗?

1 个答案:

答案 0 :(得分:0)

尝试使用[(id)textView setText:contents]