将Prev和Next按钮添加到UIPicker的Accessory视图中?

时间:2013-07-12 13:58:16

标签: ios objective-c uitextfield uipickerview

在我的视图控制器中,我有5个文本字段,4个成为带键盘的第一响应者,1个带有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但是我需要添加一个上一个和下一个按钮来使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
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分: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];
    }
}