Powered By Blogger
Showing posts with label UIPickerViewDatasource. Show all posts
Showing posts with label UIPickerViewDatasource. Show all posts

Monday, July 2, 2012

UIPickerView Datasource and Delegate methods

Hello friends ,
I'm going to share the UIPIckerView's Datasource and Delegate methods. If you want to implement these methods in to your code then you have to place "<UIPickerViewDatasource,UIPickerViewDelegate>" into your header part of the file.

@protocol UIPickerViewDataSource<NSObject>
@required

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
@end


@protocol UIPickerViewDelegate<NSObject>
@optional

// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

// these methods return either a plain UIString, or a view (e.g UILabel) to display the row for the component.
// for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
// If you return back a different object, the old one will be released. the view will be centered in the row rect 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

@end

Monday, June 11, 2012

How to put UIPickerView on UIActionSheet?

    Hello,
I'm going to explain you the combination of "UIPickerView" and "UIActionSheet".
Please check the below code. I hope you will definitely find it helpful.

UIActionSheet *actionSheet = [[[UIActionSheet alloc]initWithTitle:@"sasdasd" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
    [actionSheet showInView:self.view];
    [actionSheet setBounds:CGRectMake(0, 0, 320, 470.0)];

    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 44.0, 320.0, 250.0)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
   
    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlack;
    [pickerToolbar sizeToFit];
   
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
   
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
   
    UIBarButtonItem *btnBarCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(btnBarCancelClicked:)];
    [barItems addObject:btnBarCancel];
   
    [pickerToolbar setItems:barItems animated:YES];
   
    [actionSheet addSubview:pickerToolbar];
    [actionSheet addSubview:pickerView];

    [flexSpace release];
    flexSpace = nil;
   
    [btnBarCancel release];
    btnBarCancel = nil;
   
    [barItems release];
    barItems = nil;

    [pickerToolbar release];
    pickerToolbar = nil;
   
    [pickerView release];
    pickerView = nil;

    [actionSheet release];

If any one has query then place you it over here.

Thanks and Regards,
Nilesh Prajapati