我正在使用 selectedRowInComponent 方法获取索引为 0 的组件上当前选定的行,但它始终返回 0。我已将其签入
NSLog("%d",[pickerview selectedRowInComponent:0]);
当我以编程方式使用选择器视图时,我已经初始化了选择器。请建议。
我正在使用 selectedRowInComponent 方法获取索引为 0 的组件上当前选定的行,但它始终返回 0。我已将其签入
NSLog("%d",[pickerview selectedRowInComponent: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]]);
}
您可能想尝试同时设置它们。第二行将检索该类检测到的pickerview:
[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];