0

在我的视图控制器中,我有 5 个文本字段,4 个使用键盘成为第一响应者,一个使用 UIPicker,它们都被标记为 +1 (0,1,2...)

碰巧使用 Picker 成为第一响应者的文本字段是数字 3,所以它卡在中间。在我的其他字段中,我设置了代码,以便“下一步”返回按钮将我带到我的下一个字段,但我无法使用选择器完成相同的操作,导致这种情况发生的代码:

-(BOOL) textFieldShouldReturn:(UITextField *) theTextField {
    if (theTextField == self.textFieldE) {     
        [theTextField resignFirstResponder]; //This of course being the last text field
    } else {
        [(UITextField *)[theTextField.superview viewWithTag:theTextField.tag+1]
         becomeFirstResponder];
    }
    return YES;
} 

UIPickerView 附加了一个带有完成按钮的附件视图,实际上确实如此,resignFirstResponder但我需要能够添加一个 prev 和 next 按钮以使 firstResponder 为 -1.tag 或 +1.tag,因为选择器卡在3号位置。

这是我的选择器代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    if (textField == self.textOutlet) {

        self.textOutlet.inputView = _thePicker;
        self.thePicker.hidden = NO;

        UIToolbar* toolbar = [[UIToolbar alloc] init];
        toolbar.barStyle = UIBarStyleBlackTranslucent;
        [toolbar sizeToFit];

        //to make the done button aligned to the right
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                       style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(doneClicked:)];


        [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

        self.textOutlet.inputAccessoryView = toolbar;
    }

    }

当然还有“完成”按钮的操作:

-(void)doneClicked:(id) sender
{
    [_textOutlet resignFirstResponder]; //hides the pickerView
}

我在这里先向您的帮助表示感谢!

4

1 回答 1

1

试试这个代码。它应该可以按您的意愿工作。

#define SEGMENTED_CONTROL_TAG 1567 // random

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    if (textField == self.textOutlet) {

        self.textOutlet.inputView = _thePicker;
        self.thePicker.hidden = NO;

        UIToolbar* toolbar = [[UIToolbar alloc] init];
        toolbar.barStyle = UIBarStyleBlackTranslucent;
        [toolbar sizeToFit];

        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
        segmentedControl.tag = theTextField.tag + SEGMENTED_CONTROL_TAG;
        [segmentedControl addTarget:self action:@selector(textFieldContinue:) forControlEvents:UIControlEventValueChanged];

        //to make the done button aligned to the right
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                       style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(doneClicked:)];


        [toolbar setItems:@[segmentedControl,flexibleSpaceLeft, doneButton]];

        self.textOutlet.inputAccessoryView = toolbar;
    }

}


-(IBAction) textFieldContinue:(UISegmentedControl*)control{
    UITextField *textField;
    if(control.selectedSegmentIndex == 0) {
        textField = (UITextField*)[self.view viewWithTag:control.tag-1 - SEGMENTED_CONTROL_TAG];
    } else if (control.selectedSegmentIndex == 1) {
        textField = (UITextField*)[self.view viewWithTag:control.tag+1 - SEGMENTED_CONTROL_TAG];
    }
    if(textField) {
        [textField becomeFirstResponder];
    }
}
于 2013-07-12T14:13:44.290 回答