1

我有 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];
}
4

1 回答 1

1

您可以为选择器命名,然后区分它们为 ex 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];
}
于 2013-04-08T04:23:58.060 回答