ViewController.h
=============
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>{
int selectedProductTypeSrcIndex;
NSString *selectedProductType;
IBOutlet UIButton *productTypeOption;
}
@property(nonatomic,retain) NSArray *productTypeSrc;
@property(nonatomic,retain) UIPickerView *myPickerView;
@property(nonatomic,retain) UIPopoverController *popoverController;
- (IBAction)showOptionAction:(id)sender;
@end
ViewController.m
================
//
// ViewController.m
// PickerDemo
//
// Created by Rajendra on 27/05/13.
// Copyright (c) 2013 RajuAndroid.blogspot.com. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myPickerView,popoverController;
@synthesize productTypeSrc;
- (void)viewDidLoad
{
[super viewDidLoad];
self.productTypeSrc = [NSArray arrayWithObjects:@"Collison Damage Waive",@"Deposit Waiver",@"Excess Reduction Waiver",@"Malaysian Travel Charges Waiver",@"Penalty Waiver",nil];
selectedProductTypeSrcIndex=0;
}
- (IBAction)showOptionAction:(id)sender{
UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 260)];
UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:@"Title"];
[pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
[masterView addSubview:pickerToolbar];
CGRect pickerFrame = CGRectMake(0, 40, 300, 216);
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
[myPicker setDataSource: self];
[myPicker setDelegate: self];
myPicker.tag=1;
[myPicker selectRow:0 inComponent:0 animated:NO];
[myPicker setShowsSelectionIndicator:YES];
self.myPickerView=myPicker;
[masterView addSubview:myPicker];
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController.view = masterView;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
self.popoverController =[[UIPopoverController alloc] initWithContentViewController:viewController];
UIButton *button = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:button.bounds
inView:button
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)title {
CGRect frame = CGRectMake(0, 0, 300, 44);
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:frame];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [self createButtonWithType:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [self createButtonWithType:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
if (title){
UIBarButtonItem *labelButton = [self createToolbarLabelWithTitle:title];
[barItems addObject:labelButton];
[barItems addObject:flexSpace];
}
UIBarButtonItem *doneButton = [self createButtonWithType:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone:)];
[barItems addObject:doneButton];
[pickerToolbar setItems:barItems animated:YES];
return pickerToolbar;
}
- (IBAction)actionPickerDone:(id)sender {
//[self notifyTarget:self.target didSucceedWithAction:self.successAction origin:[self storedOrigin]];
if (self.popoverController && self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];
}
- (IBAction)actionPickerCancel:(id)sender {
if (self.popoverController && self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];
}
- (UIBarButtonItem *)createToolbarLabelWithTitle:(NSString *)aTitle {
UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = aTitle;
UIBarButtonItem *buttonLabel = [[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
return buttonLabel;
}
- (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction {
return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:target action:buttonAction];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if ([pickerView tag] == 1) {
return [self.productTypeSrc count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if ([pickerView tag] == 1) {
return [self.productTypeSrc objectAtIndex:row];
}
return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([pickerView tag] == 1) {
selectedProductTypeSrcIndex=row;
selectedProductType=[self.productTypeSrc objectAtIndex:selectedProductTypeSrcIndex];
[productTypeOption setTitle:selectedProductType forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// ViewController.m
// PickerDemo
//
// Created by Rajendra on 27/05/13.
// Copyright (c) 2013 RajuAndroid.blogspot.com. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myPickerView,popoverController;
@synthesize productTypeSrc;
- (void)viewDidLoad
{
[super viewDidLoad];
self.productTypeSrc = [NSArray arrayWithObjects:@"Collison Damage Waive",@"Deposit Waiver",@"Excess Reduction Waiver",@"Malaysian Travel Charges Waiver",@"Penalty Waiver",nil];
selectedProductTypeSrcIndex=0;
}
- (IBAction)showOptionAction:(id)sender{
UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 260)];
UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:@"Title"];
[pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
[masterView addSubview:pickerToolbar];
CGRect pickerFrame = CGRectMake(0, 40, 300, 216);
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
[myPicker setDataSource: self];
[myPicker setDelegate: self];
myPicker.tag=1;
[myPicker selectRow:0 inComponent:0 animated:NO];
[myPicker setShowsSelectionIndicator:YES];
self.myPickerView=myPicker;
[masterView addSubview:myPicker];
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController.view = masterView;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
self.popoverController =[[UIPopoverController alloc] initWithContentViewController:viewController];
UIButton *button = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:button.bounds
inView:button
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)title {
CGRect frame = CGRectMake(0, 0, 300, 44);
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:frame];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [self createButtonWithType:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [self createButtonWithType:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
if (title){
UIBarButtonItem *labelButton = [self createToolbarLabelWithTitle:title];
[barItems addObject:labelButton];
[barItems addObject:flexSpace];
}
UIBarButtonItem *doneButton = [self createButtonWithType:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone:)];
[barItems addObject:doneButton];
[pickerToolbar setItems:barItems animated:YES];
return pickerToolbar;
}
- (IBAction)actionPickerDone:(id)sender {
//[self notifyTarget:self.target didSucceedWithAction:self.successAction origin:[self storedOrigin]];
if (self.popoverController && self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];
}
- (IBAction)actionPickerCancel:(id)sender {
if (self.popoverController && self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];
}
- (UIBarButtonItem *)createToolbarLabelWithTitle:(NSString *)aTitle {
UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = aTitle;
UIBarButtonItem *buttonLabel = [[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
return buttonLabel;
}
- (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction {
return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:target action:buttonAction];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if ([pickerView tag] == 1) {
return [self.productTypeSrc count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if ([pickerView tag] == 1) {
return [self.productTypeSrc objectAtIndex:row];
}
return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([pickerView tag] == 1) {
selectedProductTypeSrcIndex=row;
selectedProductType=[self.productTypeSrc objectAtIndex:selectedProductTypeSrcIndex];
[productTypeOption setTitle:selectedProductType forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end