Search code examples
iphonepicker

how to load picker view values from sqlite database in iPhone


I'm just a beginner in iPhone programming.

I have added 3 uipickers in one view.

  1. First picker for employee.
  2. Second picker is for product.
  3. Third picker for customer.

I have created 3 tables in sqlite for the above. And I have to load the values to pickers from the tables.

Have I to keep 3 pickers in XiB file?

I want the pickers to be displayed when i click on the table cell.(I have a table view obviously...where I will choose the desired value from the picker)

For time being I have added the array statically. But I wanted to load it from the sqlite database.

How to connect?

How to display the values?

What should I do?

Can you please help me.

I've been meddling with it for 4 days. Can Someone please help....

Cathi


Solution

    1. Create 3 UiPickerView using Code after the Database read is done
    2. Set The values according to the database
    3. Set separate Tags (1,2,3) for each UIPicker. Then you can get it again every where.
    4. Set the UiPickerView delegates.

      Use [yourPicker selectRow:10 inComponent:0 animated:YES]; to set picker selected value

    Sample Code to Add 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]);
        }
    }