SingleComponent Picker in iPad

This is the Single Component Picker application. In this application we will see how to add Single Component in iPad.

Step 1: Create a View base application using template. Give the application name  ”SingleComponent_iPad”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the SingleComponent_iPadViewController class for you. Expand Resources and notice the template generated a separate nib, SingleComponent_iPadViewController.xib, for the “SingleComponent_iPad”.

Step 4: In the SingleComponent_iPadViewController.h file , we have added DataSource and delegate protocol. Create an instance of UIPickerView class and add one IBAction method. So make the following changes.

#import <UIKit/UIKit.h>

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

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

(IBAction)buttonPressed;

Step 5: Double click the SingleComponent_iPadViewController.xib file, open it to the Interface Builder. First drag the Picker view from the library and place it to the view window and drag round rect button from the library and place it to the view window. Select the picker from the view window and bring up connection inspector and drag from the datasource to the file’s owner icon, do the same thing for delegate protocol. Select the round rect button and bring up Connection Inspector and drag from the Touch Up Inside to the File’s Owner icon and select buttonPressed: action. Now save the SingleComponent_iPadViewController.xib file, close it and go back to the Xcode.

Step 6: Open the SingleComponent_iPadViewController.m file and make the following changes in the file.

(IBAction)buttonPressed
{
        NSInteger row = [singlePickerComponent 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];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
(void)viewDidLoad {
        NSArray *array = [[NSArray alloc] initWithObjects:@"SunDay",@"MonDay",@"TuesDay",@"WednesDay",@"ThusDay",
                                          @"FriDay",@"SaturDay",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 7: Now Compile and run the application in the Simulator.

You can Download SourceCode from here SingleComponent_iPad

Leave a Reply

Your email address will not be published. Required fields are marked *