Double Component Picker in iPad

This is the Double Component Picker example. In this example we will see how to worked it in the iPad.

Step 1: Create a View base application using template. Give the application name  ”DoubleComponentPicker_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 DoubleComponentPicker_ipad class for you. Expand Resources and notice the template generated a separate nib, DoubleComponentPicker_ipadViewController.xib, for the “DoubleComponentPicker_ipad”.

Step 4 : In the DoubleComponentPicker_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>
#define kFillingComponent  0
#define kBreadComponent  1

@interface DoubleComponentPicker_ipadViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
        IBOutlet UIPickerView *doublePicker;
        NSArray *fillingTypes;
        NSArray *breadTypes;
}

@property (nonatomic,retain)  UIPickerView *doublePicker;
@property (nonatomic,retain)  NSArray *fillingTypes;
@property (nonatomic,retain)  NSArray *breadTypes;

(IBAction)buttonPressed;

Step 5: Double click the DoubleComponentPicker_ipadViewController.xib file and 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 DoubleComponentPicker_ipadViewController.xib file, close it and go back to the Xcode.

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

(IBAction)buttonPressed
{
        NSInteger breadRow = [doublePicker selectedRowInComponent:
                              kBreadComponent];
        NSInteger fillingRow = [doublePicker selectedRowInComponent:
                                kFillingComponent];
        NSString *bread = [breadTypes objectAtIndex:breadRow];
        NSString *filling = [fillingTypes objectAtIndex:fillingRow];
       
        NSString *message = [[NSString alloc] initWithFormat:
                                                 @"Your %@ on %@ bread will be right up.",filling, bread];
       
        UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Thank you for your order" message:message delegate:nil cancelButtonTitle:@"Great!" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [message release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
(void)viewDidLoad {
        NSArray *breadArray = [[NSArray alloc] initWithObjects:
                                                   @"White",@"Whole Wheat",@"Rye",@"Sourdough",@"Seven Grain", nil];
        self.breadTypes = breadArray;
        [breadArray release];
       
        NSArray *fillingArray = [[NSArray alloc] initWithObjects:
                                                         @"Turkey",@"Peanut Butter",@"Tuna Salad",@"Chicken Salad",@"Roast Beef",
                                                         @"Vegemite",nil];
        self.fillingTypes = fillingArray;
        [fillingArray release];
        [super viewDidLoad];
}

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

(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
        if (component == kBreadComponent)
                return[self.breadTypes count];
        return[self.fillingTypes count];
}

(NSString *)pickerView:(UIPickerView *)pickerView
                        titleForRow:(NSInteger)row
                   forComponent:(NSInteger)component
{
        if (component == kBreadComponent)
                return [self. breadTypes objectAtIndex:row];
        return [self.fillingTypes objectAtIndex:row];
}

Step 7: Its all done, now compile and run the application in the Simulator.

You can Download SourceCode from here DoubleComponentPicker_ipad

Leave a Reply

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