Multiple Pickerview with differents values - objective-c

I have 5 tables, and I have to make an UIPickerView for each one.
I created one, like this
How can I do with the 4 others table ? I have to make 4 copy of this code ?
I do not know if I'm the right way to create my UIPickerView , I would like confirmation (or not).
This is what I did (I took the code from my colleague, who is also a beginner)
This is the right way to do ?
How to adapt it if I UIPickerViews 5 ?
#property (nonatomic, strong) UITextField *pickerViewDossier;
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerViewDossier = [[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.pickerViewDossier];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
self.pickerViewDossier.inputView = pickerView;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Valider" style:UIBarButtonItemStyleDone target:self action:#selector(doneTouched:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Annuler" style:UIBarButtonItemStyleDone target:self action:#selector(cancelTouched:)];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
self.pickerViewDossier.inputAccessoryView = toolBar;
self.listeSuiviDossier = [NSArray arrayWithObjects:#"Vous-même", #"Un confrère",nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return listeSuiviDossier.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *titrePickerview = listeSuiviDossier[row];
return titrePickerview;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *titrePickerview = listeSuiviDossier[row];
[suiviDossierBouton setTitle:titrePickerview forState:UIControlStateNormal];
}
- (IBAction)suiviDossierAction:(id)sender {
[self.pickerViewDossier becomeFirstResponder];
}
- (void)cancelTouched:(UIBarButtonItem *)sender
{
[self.pickerViewDossier resignFirstResponder];
[suiviDossierBouton setTitle:#"Sélectionnez" forState:UIControlStateNormal];
}
- (void)doneTouched:(UIBarButtonItem *)sender
{
[self.pickerViewDossier resignFirstResponder];
}

Use tag for picker so that you can select what to do for each picker.
For example:
Where you are Declaring your pickers:
picker1.tag = 1
picker2.tag = 2
and for each method:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) {
if(pickerView.tag == 1) // you can also use a switch
{
// picker 1
}
else if(pickerView.tag == 2)
{
// picker 2
}
else if(pickerView.tag == 3)
{
// picker 3
}
else if(pickerView.tag == 4)
{
// picker 4
}
}

Related

pickerview is not showing the data stored in array

#import "ViewController.h"
#interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
int selectedTextfieldValue;
NSArray *namesArray , *genderArray, *ageArray;
UIToolbar *pickerToolbar;
}
#end
#implementation ViewController
#synthesize myPickerView;
-(void)viewDidLoad
{
[super viewDidLoad];
namesArray=[[NSArray alloc]initWithObjects:#"a",#"b", nil];
genderArray=[[NSArray alloc]initWithObjects:#"M",#"F", nil];
ageArray=[[NSArray alloc]initWithObjects:#"26",#"25", nil];
selectedTextfieldValue=0;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
if (textField==_text1)
{
_text1.delegate=self;
}
else
if(textField==_text2)
{
_text2.delegate=self;
}
else
{
_text3.delegate=self;
}
NSLog(#"%d",textField.tag);
selectedTextfieldValue=(int)textField.tag;
NSLog(#"%d",selectedTextfieldValue);
// call picker here according to textfield tag you can set text to textfiled
[self popoverWithInformation];
}
-(void)popoverWithInformation
{
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 220, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
[barItems addObject:cancelBtn];
/* UIBarButtonItem flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[barItems addObject:flexSpace];/
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
myPickerView = [[UIPickerView alloc] init];
myPickerView.showsSelectionIndicator = YES;
CGRect pickerRect = myPickerView.bounds;
myPickerView.bounds = pickerRect;
myPickerView.frame = CGRectMake(0, 44, 320, 216);
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 300)];
popoverView.backgroundColor = [UIColor whiteColor];
[popoverView addSubview:myPickerView];
[popoverView addSubview:pickerToolbar];
[self.view addSubview:popoverView];
}
// tell the picker how many rows are available for a given component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
int count = 0;
switch (selectedTextfieldValue)
{
case 1:
count =namesArray.count;
NSLog(#"%d",count);
break;
case 2:
count =genderArray.count;
NSLog(#"%d",count);
break;
case 3:
count =ageArray.count;
NSLog(#"%d",count);
break;
default:
NSLog(#"default in count");
break;
}
return count;
}
// tell the picker how many components it will have
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
switch (selectedTextfieldValue) {
case 1:
title =namesArray[row];
break;
case 2:
title =genderArray[row];
break;
case 3:
title =ageArray[row];
break;
default:
NSLog(#"default");
break;
}
return title;
}
// tell the picker the width of each row for a given component
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
-(void)pickerDone:(id)sender
{
NSLog(#"Done Clicked");
NSLog(#"Done Clicked%d",selectedTextfieldValue);
myPickerView.hidden=YES;
pickerToolbar.hidden=YES;
}
-(void)pickerCancel:(id)sender
{
NSLog(#"cancel");
myPickerView.hidden=YES;
pickerToolbar.hidden=YES;
}
#end
I want to display the values of array in picker view and for that I take 3 arrays and 3 textfield but picker view not showing the data.
any help would be highly appreciated.
In this you don't need to make PickerView every time. Make first time pickerview and reload components every time
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
if (textField==_text1)
{
_text1.delegate=self;
} else if(textField==_text2) {
_text2.delegate=self;
} else {
_text3.delegate=self;
}
selectedTextfieldValue=(int)textField.tag;
[myPickerView reloadAllComponents];
}
May be it's help full other wise visit here UIPickerView programatic & UIPickerView Tutorial

Picker On AlertView Not Visible in IOS7

My below code working fine in below iOS 7
I am trying to add picker view in alert that works fine in below ios7 version But in ios 7 it show white alert without picker view.
-(void)showPinPickerAlert
{
numberarr = [[NSMutableArray alloc]initWithObjects:#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9", nil];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"Enter Pin To Change Track me Option" message:#"\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Verify" otherButtonTitles:nil];
UIPickerView *picker=[[UIPickerView alloc]initWithFrame:CGRectMake(25, 30, 230, 60) ];
picker.dataSource=self;
picker.delegate=self;
// picker.backgroundColor=[UIColor blueColor];
picker.showsSelectionIndicator = YES;
// picker.autoresizingMask = UIViewAutoresizingFlexibleHeight;
picker.transform = CGAffineTransformMakeScale(0.6, 0.6);
alert.tag=100;
// picker.transform = CGAffineTransformMakeScale(1, 0.2);
[alert addSubview:picker];
[alert show];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 4;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
strPin=[[NSString stringWithFormat:#"%i%i%i%i",[pickerView selectedRowInComponent:0],[pickerView selectedRowInComponent:1],[pickerView selectedRowInComponent:2],[pickerView selectedRowInComponent:3]]mutableCopy];
NSLog(#"strPin=%#",strPin);
// mlabel.text= [arrayNo objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [numberarr count];
}
**//code try to change color of picker text and background thats work fine in below ios7 but in iOS 7 still shows white screen**
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = (UILabel*) view;
if (label == nil)
{
label = [[UILabel alloc] init];
}
//[label setText:#"Whatever"];
// This part just colorizes everything, since you asked about that.
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor blackColor]];
CGSize rowSize = [pickerView rowSizeForComponent:component];
CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
[label setFrame:labelRect];
return label;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
pickerView.backgroundColor=[UIColor blackColor];
return [numberarr objectAtIndex:row];
}
OutPut in below
Output in IOS 7
How to fix it?
Adding subviews to a UIAlertView is not supported anymore, starting in iOS7.
You should implement your own view and try to do it similar to the alert view, or like other person said, use a 3rd party alert view like this one:
https://github.com/wimagguc/ios-custom-alertview
Most of the following code is from pre ios7 stack overflow answers.
PickerPrompt.h
#import <UIKit/UIKit.h>
#interface PickerPrompt : UIAlertView <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
{
UIPickerView *_pickerView;
NSMutableArray *_options;
}
#property (readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message options:(NSMutableArray*)options delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
#end
pickerPrompt.m
#import "PickerPrompt.h"
#implementation PickerPrompt
#define VIEW_TAG 49
#define SUB_LABEL_TAG 52
#define LABEL_TAG 53
#define COMPONENT_WIDTH 250
#define LABEL_WIDTH 10
#synthesize enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message options:(NSMutableArray*)options delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
_options = options;
_pickerView = [[UIPickerView alloc] init];
[_pickerView sizeToFit];
[_pickerView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[_pickerView setDelegate:self];
[_pickerView setDataSource:self];
[_pickerView setShowsSelectionIndicator:TRUE];
// Change from pre iOS 7
[self setAlertViewStyle:UIAlertViewStylePlainTextInput];
[[self textFieldAtIndex:0] setDelegate:self];
[[self textFieldAtIndex:0] setInputView:_pickerView];
[[self textFieldAtIndex:0] becomeFirstResponder];
}
return self;
}
#pragma mark -
#pragma mark Picker delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == _pickerView) {
return [_options count];
}
return [_options count];
}
- (UIView *)labelCellWithWidth:(CGFloat)width rightOffset:(CGFloat)offset {
// Create a new view that contains a label offset from the right.
CGRect frame = CGRectMake(0.0, 0.0, width, 32.0);
UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
view.tag = VIEW_TAG;
frame.size.width = width - offset;
UILabel *subLabel = [[UILabel alloc] initWithFrame:frame];
subLabel.textAlignment = UITextAlignmentRight;
subLabel.backgroundColor = [UIColor clearColor];
subLabel.font = [UIFont systemFontOfSize:24.0];
subLabel.userInteractionEnabled = NO;
subLabel.tag = SUB_LABEL_TAG;
[view addSubview:subLabel];
[subLabel release];
return view;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *fullString = [[textField text] stringByAppendingString:string];
for (NSString* object in _options) {
if ([object isEqualToString:fullString]) {
return YES;
}
}
return NO;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView *returnView = nil;
if ((view.tag == VIEW_TAG) || (view.tag == LABEL_TAG)) {
returnView = view;
}
else {
returnView = [self labelCellWithWidth:COMPONENT_WIDTH rightOffset:LABEL_WIDTH];
}
// The text shown in the component is just the number of the component.
NSString *text = [_options objectAtIndex:row];
// Where to set the text in depends on what sort of view it is.
UILabel *theLabel = nil;
if (returnView.tag == VIEW_TAG) {
theLabel = (UILabel *)[returnView viewWithTag:SUB_LABEL_TAG];
}
else {
theLabel = (UILabel *)returnView;
}
theLabel.text = text;
return returnView;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return COMPONENT_WIDTH;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[[self textFieldAtIndex:0] setText:[_options objectAtIndex:row]];
}
- (NSString *)enteredText
{
return [[self textFieldAtIndex:0] text];
}
#end
How to use (called from an alert view delegate):
PickerPrompt *prompt = [PickerPrompt alloc];
NSMutableArray *options = [[NSMutableArray alloc] initWithObjects:#"option 1", #"option 2", nil];
prompt = [prompt initWithTitle:#"Select Option" message:#"Select Option" options:options delegate:self cancelButtonTitle:#"Cancel" okButtonTitle:#"Okay"];
[prompt show];
[prompt release];

UIPickerView not scrolling

here is my code, I used Google map and UIPickerView:
-(void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:14];
mapViewCustomer = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapViewCustomer.myLocationEnabled = YES;
// mapView_ = [[GMSMapView alloc]initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height)];
// [self.view addSubview:mapView_];
self.view = mapViewCustomer;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"My location";
;
marker.map = mapViewCustomer;
[marker setIcon:[UIImage imageNamed:#"location1.png"]];
}
and I have a UIPickerView in viewDidLoad, I tried put it on viewWillDisappear or viewWillApear but it doesn't work:
- (void)viewDidLoad
{
pickerViewCustomer = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 162.0, 320.0, 120.0)];
//[pickerViewCustomer setFrame:CGRectMake(0.0, 162.0, 320.0, 120.0)];
pickerViewCustomer.hidden = YES;
pickerViewCustomer.delegate = self;
pickerViewCustomer.dataSource = self;
pickerViewCustomer.userInteractionEnabled = NO;
arrayDistanceFilters = [[NSArray alloc] initWithObjects:#"5km",#"10km",#"50km",#"100km",#"100km",#"100km",#"100km",#"100km",#"100km",#"100km", nil];
arrayDistanceValues = [[NSArray alloc] initWithObjects:[NSNumber numberWithFloat:5], [NSNumber numberWithFloat:10],[NSNumber numberWithFloat:50], [NSNumber numberWithFloat:100], nil];
[pickerViewCustomer selectRow:1 inComponent:0 animated:NO];
[pickerViewCustomer reloadAllComponents];
[self.view addSubview:pickerViewCustomer];
}
delegate and data source
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [arrayDistanceFilters count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [arrayDistanceFilters objectAtIndex:row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 40;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}
But scrolling does not work. What is wrong with my code?
try an other position on the view...
I had also a picker, which wasnt scrolling,too, then i moved the position(in the storyboard) of the picker and it worked...
to switch off autolayout helped also!
not satisfying, but a solution...
Please check this working example for more details
working tutorial

Picker View does not reappear once dismissed, and text field clicked a second time

Here's a simplified code, with a view controller with just a text field I also updated the title to reflect the newer issue.
When run, I click the text field and it brings up the picker view, and I'm able to click a value on the picker view and it shows up on the text field. I'm able to dismiss the picker view now when I press the Done button on the navigation bar. But now when I click the text field a second time, I don't get the picker view to pop up a second time.
import
#interface MyPickerViewViewController : UIViewController <UIPickerViewDelegate,
UIPickerViewDataSource,
UITextFieldDelegate>
#end
#interface MyPickerViewViewController () {
UIPickerView *_pv;
NSArray *_array;
IBOutlet __weak UITextField *_tf;
}
#end
#implementation MyPickerViewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_array = [NSArray arrayWithObjects:#"One", #"Two", #"Three", nil];
// Do any additional setup after loading the view, typically from a nib.
_pv = [[UIPickerView alloc] initWithFrame:CGRectZero];
_pv.dataSource = self;
_pv.delegate = self;
_tf.inputView = _pv;
UIToolbar* 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];
_tf.inputAccessoryView = mypickerToolbar;
_tf.delegate = self;
}
- (void)pickerDoneClicked:(id)sender
{
[_pv removeFromSuperview];
[_tf.inputView removeFromSuperview];
[_tf.inputAccessoryView removeFromSuperview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Added this method and now the picker view comes up every time I
// click on the text field, even after I dismiss it. However, the
// picker view is in wrong place, so I just have to adjust the
// CGRect assigned to it.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view addSubview:_tf.inputAccessoryView];
[self.view addSubview:_tf.inputView];
return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"Hello there");
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
{
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:( NSInteger)component
{
return [_array objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _array.count;
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
_tf.text = [_array objectAtIndex:row];
}
#end
I think you missed this:
[datePicker removeFromSuperview];
[pickerView removeFromSuperview];
You may try:
[self.view endEditing:YES];
in your action method
See update with this addition
// Added this method and now the picker view comes up every time I
// click on the text field, even after I dismiss it. However, the
// picker view is in wrong place, so I just have to adjust the
// CGRect assigned to it.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view addSubview:_tf.inputAccessoryView];
[self.view addSubview:_tf.inputView];
return NO;
}
Here is an adjusted CGRect for the pickerview and tool bar that worked. I was wondering though if there is a default height for a pickerview?
- (void)viewDidLoad
{
[super viewDidLoad];
_array = [NSArray arrayWithObjects:#"One", #"Two", #"Three", #"Four", nil];
// Do any additional setup after loading the view, typically from a nib.
CGRect viewRect = [[UIScreen mainScreen] bounds];
CGFloat pvHeight = 235;
CGFloat pvYpos = viewRect.size.height - pvHeight;
CGRect pvRect = CGRectMake(viewRect.origin.x, pvYpos, viewRect.size.width, pvHeight);
_pv = [[UIPickerView alloc] initWithFrame:pvRect];
_pv.dataSource = self;
_pv.delegate = self;
_tf.inputView = _pv;
CGFloat tbYpos = pvYpos-44;
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, tbYpos, viewRect.size.width, 44)];
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];
_tf.inputAccessoryView = mypickerToolbar;
_tf.delegate = self;
}

UIPickerView in a UIActionSheet: UI freezes

I'm having an issue with my iOS app freezing whenever I trigger a UIActionSheet with a UIPickerView inside it. The picker wheel scrolls fine until I try to hit the "Done" button on the UIActionSheet, at which point the UI freezes. However, XCode isn't registering any kind of crash in the program, so I'm pretty confused.
Has anyone else run into this problem before? How can I solve it?
I never Face this type of problem.I think this one is hekp you to solve your problem. I used the PickerView in same way
UIActionSheet *actionSheet;
NSString *pickerType;
- (void)createActionSheet {
if (actionSheet == nil) {
// setup actionsheet to contain the UIPicker
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}
-(IBAction)BtnPressed:(id)sender
{
[self createActionSheet];
pickerType = #"picker";
select = NO;
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
sessoTxt.text = [sessoArray objectAtIndex:0];
[chPicker release];
}
#pragma mark UIPickerViewDelegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int count;
if ([pickerType isEqualToString:#"picker"])
count = [array count];
return count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:#"picker"])
string = [array objectAtIndex:row];
return string;
}
// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
select = YES;
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:row];
}
}
- (void)pickerDone:(id)sender
{
if(select == NO)
{
if ([pickerType isEqualToString:#"picker"])
{
Txt.text = [array objectAtIndex:0];
}
}
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionSheet release];
actionSheet = nil;
}
}