【问题标题】:Adding a Prev and Next button to the Accessory view of my UIPicker?在我的 UIPicker 的附件视图中添加上一个和下一个按钮?
【发布时间】:2013-07-12 13:58:15
【问题描述】:

在我的视图控制器中,我有 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 但我需要能够添加一个上一个和下一个按钮以使 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
}

提前感谢您的帮助!

【问题讨论】:

    标签: ios objective-c uitextfield uipickerview


    【解决方案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];
        }
    }
    

    【讨论】:

    • 好吧,我尝试了代码,但是当我使用选择器选择文本字段时应用程序崩溃了。我将 theTextField 替换为具有选择器输入的文本字段的名称。我得到以下调试日志:2013-07-12 11:34:38.979 calcApp[1519:907] -[UISegmentedControl view]: unrecognized selector sent to instance 0x1dd7bfa0 2013-07-12 11:34:38.981 calcApp[1519:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISegmentedControl view]: unrecognized selector sent to instance 0x1dd7bfa0' @Vojtech Vrbka
    • 你在 UIViewController 或者你的自定义 UITextField 类中有这个函数吗?
    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多