当我选中uitextfield时,我可以显示uipickerview,并且可以选择一行来为文本字段指定选择。在此之前没有问题,您可以在下面找到代码;
-(void)pickerDoneClicked
{
[il resignFirstResponder];
mypickerToolbar.hidden=YES;
pktStatePicker.hidden=YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
return YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
return [arrState count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrState objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
il.text = [arrState objectAtIndex:row];
NSInteger asa= [pickerView selectedRowInComponent:0];
[ilsecid setText:[NSString stringWithFormat:@"%li",(long)asa+1]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
arrState= [[NSArray alloc] initWithObjects: @"class1",@"class2", @"class4",@"class10",@"class11",@"class14",@"class15",@"class16", nil];
pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)];
pktStatePicker.delegate = self;
pktStatePicker.dataSource = self;
[pktStatePicker setShowsSelectionIndicator:YES];
il.inputView = pktStatePicker ;
mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 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];
il.inputAccessoryView = mypickerToolbar;
[self invokeService];
}
但是当我再次选中文本字段时,uipickerview不再显示。 (它在第一次使用后消失,即使我再次标记也没有显示。
答案 0 :(得分:0)
是bobnoble,textFieldShouldBeginEditing需要将工具栏和pickerview隐藏为NO。谢谢你指出我的遗漏。