Thursday 8 November 2012

Add a UIPickerView in UIActionSheet for iOS

.h File

@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate>
{
    
    IBOutlet UITextField *txtText;
  NSArray *arrState;
    UIPickerView *pktStatePicker;
    UIToolbar *mypickerToolbar;
    UIActionSheet *sheet;
}

-(IBAction)open:(id)sender;


@end


.m File

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad

{
    
    [super viewDidLoad];
    
    arrState=[[NSArray alloc]initWithObjects:@"NewYork",@"NewJercy",@"Carlifornia",@"Florida",@"Fremont",@"SantaClara",@"San Diego",@"San Fransisco",@"San Jose", nil];
   
    
    pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43 , 320, 480)];
    
    pktStatePicker.delegate = self;

    pktStatePicker.dataSource = self;
    
    [pktStatePicker  setShowsSelectionIndicator:YES];
    
    
    
    // Create done button in UIPickerView
    
    
    mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
    
    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
    
    [mypickerToolbar sizeToFit];
    
    
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
    
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    [barItems addObject:flexSpace];
    
    
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
    
    [barItems addObject:doneBtn];
    
    
    [mypickerToolbar setItems:barItems animated:YES];
    
}


-(IBAction)open:(id)sender
{
    sheet = [[UIActionSheet alloc] initWithTitle:nil
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];
    [sheet addSubview:pktStatePicker];
    [sheet showInView:self.view.superview];
    [sheet addSubview:mypickerToolbar];
    [sheet showInView:self.view.superview];
    
    [sheet setBounds:CGRectMake(0, 20, 320, 430)];
    
    [pktStatePicker release];
    [mypickerToolbar release];
    [sheet release];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{
    
    return 1;
    
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    
    return [arrState count];
    
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
    
    return [arrState objectAtIndex:row];
    
}


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

{
    NSLog(@"You selected this: %@", [arrState objectAtIndex: row]);
    txtText.text = [arrState objectAtIndex:row];
    
}


- (void)pickerDoneClicked
{
    NSLog(@"Done Clicked");
    
    [txtText resignFirstResponder];
    
    [sheet dismissWithClickedButtonIndex:0 animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end



Xib File:



Output:






3 comments:

Unknown said...

Hi ! Really interesting. Do you have a test project?

Unknown said...

Thanks AsHuss .. thanks very much

sohil multani said...

This is not work ...only display Done
button