ios - UIPickerView 未正确显示数据(仅显示 "?")

标签 ios objective-c uipickerview uipickerviewcontroller uipickerviewdatasource

如果这个问题之前已经讨论过,请原谅我。
我在 UIPickerView 中显示数据时遇到问题,它只显示问号“?”

头文件.h

#import <UIKit/UIKit.h>
@interface CustomPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
    NSMutableArray *arrayColors;
}

@property (strong, nonatomic) IBOutlet UIPickerView *kotaPicker;

@end

实现.m

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayColors count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row {
    return [arrayColors objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"The Data is %@", [arrayColors objectAtIndex:row]);
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    arrayColors = [[NSMutableArray alloc] init];
    [arrayColors addObject:@"Red"];
    [arrayColors addObject:@"Orange"];
    [arrayColors addObject:@"Yellow"];
    [arrayColors addObject:@"Green"];
    [arrayColors addObject:@"Blue"];
    [arrayColors addObject:@"Indigo"];
    [arrayColors addObject:@"Violet"];
    // Do any additional setup after loading the view from its nib.
}

最佳答案

好的,你的问题解决了,问题是委托(delegate)方法不正确,

您使用的方法(用于加载数据)不是委托(delegate)方法,

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row

试试这个方法,(一定有效)

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
     return [arrayColors objectAtIndex:row];
}

关于ios - UIPickerView 未正确显示数据(仅显示 "?"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376930/

相关文章:

iphone - 指向整数转换的不兼容指针将 'void *' 发送到类型为 'NSJSONReadingOptions' 的参数

ios - 如何在 UITableView 中使用 UITableView 作为 ios Objective-C 中的下拉列表

objective-c - 如何使用关闭按钮关闭 UIActionSheet 并避免崩溃?

swift - UIPickerViewDelegate Xcode 8 swift 3

ios - 如何在Swift 3中禁用选项卡栏的用户交互?

ios - Xcode 11 构建系统有问题?

PHP IOS 推送通知(权限被拒绝)

ios - 魔法记录 : crash when fetching data in background thread

iphone - 如何加载大型本地镜像(适用于模拟器,而不是测试设备)?

ios - 如何检测 UIPickerView 的变化?