在UITableView中隐藏UIPickerview On Done按钮

时间:2012-04-03 06:28:02

标签: iphone ios ipad

我在UITableview的每个Cell中都有UITextField,我已将UIPickerview添加为UITextField的inputView,并在其工具栏上显示Done按钮

我的问题是如何在点击完成按钮时隐藏此弹出窗口(Picker +工具栏)? 并在特定单元格的文本框中显示选择器的选定值?

谢谢和问候

编辑:代码

- (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;

}

解决: 我已经将currentTextBox作为变量并添加了以下方法并在完成按钮的单击中调整其第一个响应者的大小:)

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

3 个答案:

答案 0 :(得分:1)

UIPopOver无法从他们的班级被解雇,你需要将其从调用班中解雇。 当用户按下完成按钮时,你必须从popover调用类调用dismiss方法

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

我认为这可以解决您的问题 对于您的输入视图 -

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

别忘了保存当前选定的文本字段。

答案 1 :(得分:0)

假设你把UIPickerView放在一个popover中,这是怎么做的:

  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];
}

答案 2 :(得分:0)

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