简体   繁体   English

在UITableView中的“完成”按钮上隐藏UIPickerview

[英]Hide UIPickerview On Done Button in UITableView

I have UITextField in each Cell of UITableview and I have added UIPickerview as inputView of UITextField and showing with Done button at its tool bar 我在UITableview的每个单元格中都有UITextField,并且已将UIPickerview添加为UITextField的inputView,并在其工具栏上显示“完成”按钮

My question is how can I hide this this pop up (Picker + toolbar) on click of done button ? 我的问题是,单击完成按钮后如何隐藏此弹出窗口(选择器+工具栏)? and show selected value of picker in text box in particular cell ? 并在特定单元格的文本框中显示选择器的选定值?

Thanks and Regards 谢谢并恭祝安康

Edit : Code 编辑:代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
        PremiumProductsDescriptionCell *cell = (PremiumProductsDescriptionCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[PremiumProductsDescriptionCell alloc] initShoppingCartCellWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

ShopProduct *p = (ShopProduct *)[[ShopProduct GetShoppingCart] objectAtIndex:indexPath.row];

cell.Quantity.text = [NSString stringWithFormat:@"%d",p.Quantity];

    UIPickerView *quantityPicker = [[UIPickerView alloc] init];
    quantityPicker.dataSource = self;
    quantityPicker.delegate = self;
    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
                            CGRectMake(0,0, 320, 44)]; 

    UIBarButtonItem *doneButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                  target:self action:@selector(hideKeyBoard)];

quantityPicker.tag = indexPath.row;
    [myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];

    cell.Quantity.inputAccessoryView = myToolbar;

    cell.Quantity.inputView = quantityPicker;


    cell.Quantity.delegate = self;

    return cell;

}

Solved : I have taken currentTextBox a variable and added following method and resizing its first responder in done button's click :) 解决:我已将currentTextBox用作变量,并添加了以下方法,并在完成按钮的单击中调整了其第一响应者的大小:)

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    currentTextBox = textField;
}

UIPopOver cant be dismissed from their class and you need to dismiss it from the calling class. UIPopOver无法从他们的班级被解雇,您需要从调用者班级将其解雇。 You have to call dismiss method from popover calling class, when user presses the done button 当用户按下完成按钮时,必须从弹出调用类中调用dismiss方法

-(void)doneButtonClikd
   { ParentClass *viewController=[ParentClass alloc]init];
     [viewController dismissPopOver];
    }

I think this will solve your problem For your inputview- 我认为这可以解决您的问题。

   -(void)doneButtonclikd 
        {  [selectedTextfield resignFirstResponder];
         }

Dont forget to save the currently selected textfield. 不要忘记保存当前选定的文本字段。

Assuming you put the UIPickerView in a popover, here's how to do it: 假设您将UIPickerView放在弹出窗口中,请按照以下步骤操作:

  UIPopoverController* popover = ....
  UIBarButtonItem* doneButton = ....
  [doneButton addTarget:self action:@selector(closeMe) 
      forControlEvents:UIControlEventTouchUpInside]
  // ....


- (void)closeMe
{
  // Assuming popover is really a field or something...
  [popover dismissPopoverAnimated:YES];
}

使用[self.view endEditing:YES]方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM