【问题标题】:UIPickerView select and hideUIPickerView 选择和隐藏
【发布时间】:2011-03-18 02:51:11
【问题描述】:

你如何让UIPickerView 像带有 web 视图的那个一样,其中有一个下拉选择框,而不是像通常的网站那样下拉,iphone 使它成为 UIPickerView 与所有选择。当您选择一个时,您的选择旁边会出现一个复选标记并更改下拉框的值。以及如何将“完成”按钮放在UIPickerView 顶部以关闭UIPickerView

我已经知道[pickerview setHidden:YES] 是用来隐藏pickerview 的方法。我只是不知道如何在UIPickerView 中包含“完成”按钮。

问候, 克里斯

【问题讨论】:

    标签: iphone ios uipickerview


    【解决方案1】:

    这段代码将作为键盘滑出一个选择器视图,并在其顶部附加一个完成按钮。基本上,你想用你的输入字段设置一个 inputAccessoryView。 您应该在输入字段的触摸事件上调用此方法。

    - (IBAction)showYourPicker:(id)sender {
    
    // create a UIPicker view as a custom keyboard view
    UIPickerView* pickerView = [[UIPickerView alloc] init];
    [pickerView sizeToFit];
    pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    self.yourPickerView = pickerView;  //UIPickerView
    
    yourTextField.inputView = pickerView;
    
    // create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
    // Prepare done button
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle = UIBarStyleBlack;
    keyboardDoneButtonView.translucent = YES;
    keyboardDoneButtonView.tintColor = nil;
    [keyboardDoneButtonView sizeToFit];
    
    UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
        style:UIBarButtonItemStyleBordered target:self
        action:@selector(pickerDoneClicked:)] autorelease];
    
    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
    
    // Plug the keyboardDoneButtonView into the text field...
    yourTextField.inputAccessoryView = keyboardDoneButtonView;  
    
    [pickerView release];
    [keyboardDoneButtonView release];
    }
    

    最后,您的完成按钮调用“pickerDoneClicked”方法,您应该在其中添加 [yourTextField resignFirstResponder]; 将隐藏选择器视图。

    【讨论】:

    • 我错过了什么让你自己回答?
    • 对不起,我只是觉得可以选择不同的方法很好。一种是在屏幕底部有一个工具栏,另一种是在键盘/uipickerview 顶部附加一个工具栏。
    • 感谢雅各布提供的额外信息。这真的很有帮助,但我相信最终方法的命令是 [mypicker setHidden:YES];我说的对吗?
    • @Tofu 我不这么认为。由于当文本字段不再是第一响应者时,选择器显示为文本字段的 inputView,因此视图将自动隐藏。
    • 这个答案要好得多,因为它清楚地显示了 UITextField 和 UIPIckerView 之间的联系
    【解决方案2】:

    “完成”按钮放置在 UIToolBar 中。

    使用下面的 UIToolBar 方法添加“完成”按钮。

    - (void)setItems:(NSArray *)items animated:(BOOL)animated {
    
        UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
        mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [mypickerToolbar sizeToFit];
    
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
    
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
        [barItems addObject:doneBtn];
    
        [mypickerToolbar setItems:barItems animated:YES];
    
    }
    

    【讨论】:

    • 弹性空间的用途是什么?即使没有它似乎也能工作?很抱歉对旧线程发表评论。
    • 我在这里看不到与 UIPickerView 的连接。
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2014-08-12
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    相关资源
    最近更新 更多