【问题标题】:How to hide a UIPickerView when the user make its choice用户做出选择时如何隐藏 UIPickerView
【发布时间】:2012-12-01 06:34:31
【问题描述】:

我使用以下代码创建了一个自定义 UIPickerView

UIPickerView *picker =[[UIPickerView alloc] initWithFrame:CGRectMake(139,50,161,30)];
    picker.delegate=self;
    picker.showsSelectionIndicator=YES;
    picker.hidden=NO;

    [self.view addSubview:picker];

现在我想在用户选择行时隐藏pickerView

picker.hidden=YES;

现在: 1)如何识别用户的选择,然后隐藏无用的pickerview? 2) 我可以在 TextField 中显示选择吗?用@“选择”?

【问题讨论】:

    标签: iphone xcode hide uipickerview


    【解决方案1】:

    在 XCode 12.2 中,您可以使用:

    thePickerView.isHidden = true
    

    【讨论】:

      【解决方案2】:

      我使用 UIPickerView 作为 TextField 的 inputView(用于替换标准键盘)。我使用 ResignFirstResponder(TextField 方法)来关闭视图。

      【讨论】:

        【解决方案3】:

        UIView *addMealView; // 这里我展示了我的选择器

           - (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component 
        {   
        [addMealView endEditing:YES];   
        }
        

        【讨论】:

          【解决方案4】:

          我使用 UIPickerView 作为 TextField 的 inputView(因此替换了屏幕键盘)。我使用以下委托来关闭视图。

          #pragma mark - UIPickerViewDelegate
          - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
              // Code logic
              [[self view] endEditing:YES];
          }
          

          【讨论】:

            【解决方案5】:

            将此行添加到您创建选择器的位置

            picker.delegate = self;
            

            你的回调

            - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
                picker.hidden=YES;
            }
            

            【讨论】:

            • 当我添加这一行时:没有可见的@interface for 'UIPickerView' 声明选择器“addTarget:action...” 我已经在函数中声明了选择器。我该如何解决?跨度>
            • 我已经在 .h 中将选择器声明为 uipickerview 并将其从函数中删除以不隐藏在 .h 中声明的 istance 变量,但问题仍然存在
            • @AlessandroBava 抱歉与日期选择器混淆。已编辑
            • 那么就不能这样吗?
            【解决方案6】:

            像下面这样使用UIPickerView的委托方法..

               - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
            
                    yourTextField.text = [yourArray objectAtIndex:row];
                    thePickerView.hidden = YES;
            
                }
            

            您也可以使用UIButton 并使用UIButton 的动作事件隐藏它。

            【讨论】:

              【解决方案7】:

              您可以在 UINavigationBar 和 Barbutton 上使用取消和完成文本。

              您可以参考以下链接

              Objective C implementing a UIPickerView with a "Done" button

              【讨论】:

              • 不,我不想在视图中添加按钮。我想在用户做出选择时自动隐藏:)
              • 你将如何识别用户是否选择了正确的选择?
              • 只需双击所选行
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-12
              • 2020-08-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多