1

我只是 iPhone 编程的初学者。

我在一个视图中添加了 3 个 uipickers。

  1. 员工的第一拣货员。
  2. 第二个选择器用于产品。
  3. 客户的第三个拣货员。

我在 sqlite 中为上面创建了 3 个表。而且我必须将值加载到表中的选择器中。

我必须在 XiB 文件中保留 3 个选择器吗?

我希望在单击表格单元格时显示选择器。(我显然有一个表格视图......我将从选择器中选择所需的值)

暂时我已经静态添加了数组。但我想从 sqlite 数据库中加载它。

如何连接?

如何显示值?

我该怎么办?

你能帮我么。

我已经干预了4天了。有人可以帮忙吗....

卡西

4

1 回答 1

0
  1. 数据库读取完成后使用代码创建 3 UiPickerView
  2. 根据数据库设置值
  3. 为每个 UIPicker 设置单独的标签 (1,2,3)。然后你可以在任何地方再次获得它。
  4. 设置 UiPickerView 代表。

    使用[yourPicker selectRow:10 inComponent:0 animated:YES]; 设置选择器选择的值

添加 UIPicker 的示例代码

-(void)displayPicker{   

    @try {

        UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];

        NSMutableArray *barItems = [[NSMutableArray alloc] init];       

        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancel:)];
        cancelBtn.tag = 1;
        [barItems addObject:cancelBtn];
        [cancelBtn release];
        cancelBtn = nil;

        /*  Uncomment this line to add label to Timer/Counter. 

        NSString *popUpTitleText = [self getPopUpTitleText:@"My Text"];         
        UIBarButtonItem *titleBtn = [[UIBarButtonItem alloc] initWithTitle:popUpTitleText style:UIBarButtonItemStylePlain target:nil action:nil];
        [barItems addObject:titleBtn];
        [titleBtn release];
        titleBtn = nil;

         */

        UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace  target:nil  action:nil];
        flexItem.width = 64;                                     
        [barItems addObject:flexItem];
        [flexItem release];
        flexItem = nil;


        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
        doneBtn.tag = 1;
        [barItems addObject:doneBtn];       
        [doneBtn release];
        doneBtn = nil;

        [pickerToolbar setItems:barItems animated:YES];     
        [barItems release];
        barItems = nil;

        CGRect pickerFrame = CGRectMake(0, 40, 0, 216);     
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
        pickerView.showsSelectionIndicator = YES;
        pickerView.tag = 1;
        pickerView.dataSource = self;
        pickerView.delegate = self;     
        CGRect pickerRect = pickerView.bounds;
        pickerView.bounds = pickerRect;


        UIViewController* popoverContent = [[UIViewController alloc] init];
        UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 344)];
        popoverView.backgroundColor = [UIColor whiteColor];     
        pickerView.frame = CGRectMake(0, 44, 200, 216);


        [pickerView selectRow:1 inComponent:0 animated:YES]; //set your selected (Database) value here.

        [popoverView addSubview:pickerToolbar];
        [popoverView addSubview:pickerView];
        popoverContent.view = popoverView;

        //resize the popover view shown
        //in the current view to the view's size
        popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 244);


        //create a popover controller       
        popoverController.delegate = nil;   
        if (popoverController.popoverVisible == YES) {

            [popoverController dismissPopoverAnimated:YES];
        }
        [popoverController release];
        popoverController = nil;
        popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
        CGRect popoverRect = [self.view convertRect:yourFrame(CGrect) 
                                           fromView:[yourView superview]];
        popoverRect.origin.y = popoverRect.origin.y + 15;
        popoverController.delegate = self;
        popoverRect.size.width = MIN(popoverRect.size.width, 100) ;     

        popoverRect.origin.x = float yourXvalue;    

        [popoverController  presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

        //release the popover content
        [popoverView release];
        [popoverContent release];
        [pickerToolbar release];
        [pickerView release];

        popoverView = nil;
        popoverContent = nil;
        pickerToolbar = nil;
        pickerView = nil;   
    }
    @catch (NSException * ex) {

        NSLog(@"Exception in YourClass Method: displayPicker() Name:%@ Reason:%@",[ex name],[ex reason]);
    }
}
于 2012-09-13T07:09:08.757 回答