ios - 奇怪的字符串文本出现在 UIPickerView 内容中

标签 ios uikit nsarray uipickerview

我编写了以下代码,出于某种原因 pickerView 显示 ??????而不是它应该显示的数组。

数组已经在header中定义,dataSource已经设置到view controller。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    arrayContext = [[NSMutableArray alloc] init];
    [arrayContext addObject:@"is"];
    [arrayContext addObject:@"wants"];
    [arrayContext addObject:@"loves"];
    [arrayContext addObject:@"hates"];
    [arrayContext addObject:@"enjoyed"];
    [arrayContext addObject:@"liked"];
    [arrayContext addObject:@"likes"];

    [pickerView selectRow:1 inComponent:0 animated:NO];
}

 - (void)viewDidUnload
{
    [textField release];
    textField = nil;
    [pickerView release];
    pickerView = nil;
    [arrayContext release];
    arrayContext = nil;
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 1;
}

 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.ScotDev.QuickComPrefs.plist"];
    NSString *chosenContext = [NSString stringWithFormat:@"%@ ", [arrayContext objectAtIndex:row]];
    [plistDict setValue:chosenContext forKey:@"Context"];
}    

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return [arrayContext count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     forComponent:(NSInteger)component {

    return [arrayContext objectAtIndex:row];
}

最佳答案

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

delegate 方法而不是 dataSource 方法。为了使您的代码正常工作,两者都必须连接到此 View Controller 。

关于ios - 奇怪的字符串文本出现在 UIPickerView 内容中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9691470/

相关文章:

objective-c - 访问数组中对象的属性

ios - 在单数组中转换字典数组

ios - 如何使用 SwiftUI 调用电话

ios - self.tabBarController 有时为零

iphone - UIView翻转动画

iphone - 随机访问NSArray,但不会丢失或加倍对象

ios - 将变量从 UIViewController 传递到自定义 UITableViewCell

php - session 在 iPhone(Safari、Chrome)中不起作用

ios - 没有 StoreKit 的 Apple 应用内购买?

ios - -[NSLayoutConstraint 常量] 断言失败