pickerview selectedRowInComponent总是返回0

时间:2012-09-22 14:03:39

标签: iphone ios uipickerview

我正在使用selectedRowInComponent方法获取索引为0的组件上当前选定的行,但它始终返回0.我已在

中检查了它
 NSLog("%d",[pickerview selectedRowInComponent:0]);

我已经初步化了选择器,因为我正在以编程方式使用选择器视图。请建议。

2 个答案:

答案 0 :(得分:0)

我希望这段代码可以帮助你。我在我的项目中使用它完美的工作。

将此功能称为ViewDidLoad

[self loadFirstPicker];

下面是制作仅显示时间的UIpicker的功能。 (并保存在文本中)

-(void)loadFirstPicker
{
    UIDatePicker *picker = [[UIDatePicker alloc] init];
    picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    picker.datePickerMode = UIDatePickerModeTime;

    [picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
    CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
    picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
    picker.backgroundColor = [UIColor blackColor];
    checkEveryTextFeild.inputView = picker;

    //[self.view addSubview:picker];
    //[picker release];

    // Create done button in UIPickerView
    UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 220, 56)];
    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [mypickerToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
    [barItems addObject:doneBtn];

    [mypickerToolbar setItems:barItems animated:YES];
    checkEveryTextFeild.inputAccessoryView = mypickerToolbar;
}

此函数从选择器获取时间并将其设置为textFeild

-(void) dueDateChanged:(UIDatePicker *)sender {

    NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    //[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    checkEveryTextFeild.text = [dateFormatter stringFromDate:[sender date]];
    NSDate *currentDate = [NSDate date]; 
    NSLog(@"%@",currentDate);
    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);

}

答案 1 :(得分:0)

你可能想尝试设置它们。第二行将检索该类检测到的选择器视图:

[filterPicker selectRow:selectedPickerIndex inComponent:0 animated:NO];

[self pickerView:filterPicker didSelectRow:selectedPickerIndex inComponent:0];

self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];