当选择 UITextField 时显示 UIPickerView

发布于 2024-08-29 03:53:19 字数 53 浏览 2 评论 0原文

有没有人有一个在选择 UITextField 时显示 UIPickerView 的基本示例。

Does anyone have a basic example to display a UIPickerView when a UITextField was selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚笙 2024-09-05 03:53:19

最好的位置是在 NSTextField Delegate 方法中:

- (BOOL)textShouldBeginEditing:(NSText *)textObject

重写该方法以返回 NO 并调用选取器视图。返回 NO 将阻止键盘出现。

The best place to do so would be in the NSTextField Delegate method:

- (BOOL)textShouldBeginEditing:(NSText *)textObject

Override the method to return NO and instead evoke the picker view. Returning NO will prevent the keyboard from appearing.

橘亓 2024-09-05 03:53:19

我所做的是实现 TouchsEnded 事件。如果该事件发生在我的 UITextField 参数中,那么我会隐藏或显示 UIPickerView

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{    
    UITouch *touch = [[event allTouches] anyObject];

    if (CGRectContainsPoint([self.textField frame], [touch locationInView:self.view]))
    {
        //Want to show or hide UIPickerView
        if(pickerView)
        {
            submitButton.hidden = !submitButton.hidden;
            pickerView.hidden = !pickerView.hidden;
        }
    }
}

What I did was implement touchesEnded event. If that event occurs in my UITextField parameters then I either hide or show the UIPickerView

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{    
    UITouch *touch = [[event allTouches] anyObject];

    if (CGRectContainsPoint([self.textField frame], [touch locationInView:self.view]))
    {
        //Want to show or hide UIPickerView
        if(pickerView)
        {
            submitButton.hidden = !submitButton.hidden;
            pickerView.hidden = !pickerView.hidden;
        }
    }
}
蓝海 2024-09-05 03:53:19

http://tmblr.co/ZjkSZteCOUBS

我的博客中列出了代码和所有内容来准确执行此操作。但下面我列出了基本概念。

基本上,该解决方案涉及 github 上名为 ActionSheetPicker 的开源项目,并在 UITextFieldDelegate 上实现 textFieldShouldBeginEditing 功能。您可以在那里关闭键盘并提供 UIPickerView。基本代码列于此处:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // We are now showing the UIPickerViewer instead

    // Close the keypad if it is showing
    [self.superview endEditing:YES];

    // Function to show the picker view
    [self showPickerViewer :array :pickerTitle];

    // Return no so that no cursor is shown in the text box
    return  NO;
}

编辑我知道这个问题不久前被问过,但觉得有必要提供更新的解决方案。

http://tmblr.co/ZjkSZteCOUBS

I have the code and everything laid out in my blog to do this exactly. But below, I have the basic concept laid out.

Basically the solution involves an opensource project called ActionSheetPicker on github, and implementing the function textFieldShouldBeginEditing on the UITextFieldDelegate. You can dismiss the keyboard there and provide a UIPickerView instead. The basic code is listed here:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // We are now showing the UIPickerViewer instead

    // Close the keypad if it is showing
    [self.superview endEditing:YES];

    // Function to show the picker view
    [self showPickerViewer :array :pickerTitle];

    // Return no so that no cursor is shown in the text box
    return  NO;
}

EDIT I know this question was asked a while ago, but felt it was necessary to provide a more recent solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文