【发布时间】:2014-07-22 02:39:24
【问题描述】:
我有一个 UIPickerView,我想根据这个堆栈溢出问题中显示的方法将文本的颜色更改为白色:How do I change the color of the text in a UIPickerView under iOS 7?。
但是,我输入的代码似乎不起作用。谁能告诉我为什么?
我的 ViewController.m 中有以下方法
- (NSAttributedString *)changePickerColor:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component :(NSString*)title
{
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
return attString;
}
在我的 viewDidLoad 中,我写了这段代码:
self.instruments = [[NSMutableArray alloc]initWithObjects:@"Baritone",@"Trumpet",@"French Horn",@"Tuba", nil];
for (int i = 0; i < self.instruments.count; i++) {
[self changePickerColor:self.pickerView attributedTitleForRow:i forComponent:0 :self.instruments[i]];
}
我没有收到任何错误消息,代码根本不起作用。 请帮忙,谢谢。
【问题讨论】:
-
pickerView:attributedTitleForRow:forComponent:是协议定义的委托方法——它的命名必须完全一样(就像你链接到的答案一样)。此外,您不应该自己调用它——选择器视图会调用它(只要它的命名正确)。 -
试试这个,stackoverflow.com/questions/18807940/…这可能对你有帮助
标签: ios objective-c ios7 uipickerview