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

时间:2012-03-13 20:21:13

标签: ios uikit nsarray uipickerview

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

数组已在标题中定义,dataSource已设置为视图控制器。

 - (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];
}

1 个答案:

答案 0 :(得分:1)

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

delegate方法不是dataSource方法。为了使您的代码工作,两者都必须连接到此视图控制器。