How to implement Single Component Picker in Iphone?

Let’s look at using pickers that let the user select from a list values. In this example we are going to create an NSArray to hold the values we want to display in the picker.

 Let’s look at using pickers that let the user select from a list values. In this example we are going to create an NSArray to hold the values we want to display in the picker.

Step 1: Create a new project from Xcode and select view-base application template.

Step 2: Adding the following code to the controller Header file.

#import <UIKit/UIKit.h>

@interface SingleComponentPickerViewController : UIViewController
 <UIPickerViewDataSource , UIPickerViewDelegate>
 {
        IBOutlet UIPickerView *singlePicker;
        NSArray *pickerData;
}

@property(nonatomic , retain) UIPickerView *singlePicker;
@property(nonatomic , retain) NSArray *pickerData;

-(IBAction)buttonPressed;

Step 3: Double click .xib file and open it to interface Builder. Add a picker and a button to te View,and  then make the necessary connections. Control drag File’s Owner to the picker view,and select singlePicker outlet. Then single click the picker, bring up connection inspector, you’ll see that the first two items are DataSource and Delegate. Drag from the circle next to DataSource to the File’s Owner icon. Do it once again, and connect Delegate to File’s Owner icon. Next drag a Round Rect Button to the view, double-click it, give it a title of Select. Connect to Touch Up Inside to File’s Owner  and select buttonPressed action. Save the nib, and go back to Xcode.

Step 4: To make our controller work properly as the picker’s datasource and delegate, we are going to implement a few methods in the controller class:

-(IBAction)buttonPressed
{
        NSInteger row = [singlePicker selectedRowInComponent:0];
        NSString *selected = [pickerData objectAtIndex:row];
        NSString *title = [[NSString alloc] initWithFormat:
                         @"you selected %@!", selected];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
             message : @"Thank you for choosing."
             delegate:nil
             cancelButtonTitle :@"You are Welcome"            
             otherButtonTitles :nil];
        [alert show];
        [alert release];
        [title release];
}

- (void)viewDidLoad {
        NSArray *array = [[NSArray alloc]        initWithObjects:@"Luke",@"Leia",@"Han",@"Chewbacca",@"Artoo",
                  @"Threepio",@"lando",nil];
        self.pickerData = array;
        [array release];
        [super viewDidLoad];
}

 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   return 1;
}

  -(NSInteger)pickerView:(UIPickerView *)pickerView
  numberOfRowsInComponent:(NSInteger)component
  {
        return [pickerData count];
  }

  -(NSString *)pickerView:(UIPickerView *)pickerView
                        titleForRow:(NSInteger)row
                   forComponent:(NSInteger)component
   {
    return[pickerData objectAtIndex:row];
  }

Step 5: Now compile and run your application on simulator.(See figure 1)

Figure 1: A picker displaying single list of values.

You can downloaded SourceCode from here SingleComponentPicker 2

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

1 Response » to “How to implement Single Component Picker in Iphone?”

  1. Really , this is super iPhone Beginner Tutorials.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG