我有2个文本字段。每个都有自己的选择器视图。当用户点击每个选择器时,会出现UIPickerView。一切正常。但是,首先,当我单击第二个文本字段时,会出现第二个文本字段的选择器视图,第一个文本字段将从此选择器视图中获取结果,而不是第二个文本字段。我的代码中有些东西不正确。我无法弄清楚到底是什么。请帮忙......提前致谢)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == reminder){
choosen = reminderString;
mySheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[mySheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
myArray = [[NSMutableArray alloc]initWithObjects: @"15 minutes",@"20 minutes",@"30 minutes", @"45 minutes",nil];
CGRect pickerFrame = CGRectMake(0,44,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate =self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
[mySheet addSubview:pickerView];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mySheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spaser = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"set" style:UIBarButtonItemStyleDone target:self action:@selector(DatePickerDoneClick)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelRepetition)];
[controlToolBar setItems:[NSArray arrayWithObjects:spaser, setButton, cancelButton,nil] animated:NO];
[mySheet addSubview:controlToolBar];
[mySheet showInView:self.view];
[mySheet setBounds:CGRectMake(0, 0, 320, 485)];
return NO;
}
else if(textField == repetition){
choosen = repetitionString;
repetitionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[mySheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
myArray = [[NSMutableArray alloc] initWithObjects:@"One-time task",@"Daily",@"Weekly",@"Yearly",nil];
CGRect pickerFrame = CGRectMake(0,44,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate =self;
pickerView.dataSource=self;
pickerView.showsSelectionIndicator = YES;
[mySheet addSubview:pickerView];
UIToolbar *controlToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mySheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleBlack];
[controlToolBar sizeToFit];
UIBarButtonItem *spaser = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"set" style:UIBarButtonItemStyleDone target:self action:@selector(DatePickerDoneClick)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelRepetition)];
[controlToolBar setItems:[NSArray arrayWithObjects:spaser, setButton, cancelButton,nil] animated:NO];
[mySheet addSubview:controlToolBar];
[mySheet showInView:self.view];
[mySheet setBounds:CGRectMake(0, 0, 320, 485)];
return NO;
}
else{
return 0;
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return myArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [myArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (chosen == reminderString) {
reminderString =[myArray objectAtIndex:row];
[reminder setText:reminderString];
}
else if (chosen == repetitionString){
reminderString =[myArray objectAtIndex:row];
[repetition setText:reminderString];
}
}
- (void)DatePickerDoneClick{
[mySheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancelRepetition{
[mySheet dismissWithClickedButtonIndex:0 animated:YES];
}
答案 0 :(得分:1)
您可以将名称给予拣货员,然后区分它们 前Picker1和picker2以及didselectrow中的条件
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(pickerview == picker1)
{
reminderString =[myArray objectAtIndex:row];
[reminder setText:reminderString];
}
elseif(pickerview == picker2)
{
reminderString =[myArray objectAtIndex:row];
[repetition setText:reminderString];
}