pickerview is not showing the data stored in array - ios7

#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

Related

Multiple Pickerview with differents values

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
}
}

Unrecognized selector issue

This is the code:
#interface CreateAccountViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{
UIToolbar *keyboardToolbar;
NSDate *birthdate;
UIActionSheet *dateSheet;
}
#property(strong, nonatomic) IBOutlet UITextField *txtFirstName;
#property(strong, nonatomic) IBOutlet UITextField *txtLastName;
#property(strong, nonatomic) IBOutlet UITextField *txtGender;
#property(strong, nonatomic) IBOutlet UITextField *txtBirthdate;
#property(strong, nonatomic) IBOutlet UITextField *txtEmail;
-(void)resignKeyboard:(id)sender;
-(void)gotoPreviousField:(id)sender;
-(void)gotoNextField:(id)sender;
-(void)setBirth;
-(void)dismissDateSet;
-(void)gotoPreviousBirthdate;
-(void)gotoNextBirthdate;
#end
#implementation CreateAccountViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:YES];
[[[keyboardToolbar items] objectAtIndex:1] setEnabled:YES];
if([textField isEqual:self.txtFirstName])
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:NO];
}
else if([textField isEqual:self.txtEmail])
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:NO];
}
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if([textField isEqual:self.txtBirthdate])
{
[self setBirth];
return NO;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view
if(keyboardToolbar==nil)
{
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *btnPrevious = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoPreviousField:)];
UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextField:)];
UIBarButtonItem *btnExtraSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *btnDone=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects:btnPrevious, btnNext, btnExtraSpace, btnDone, nil]];
}
}
-(void) resignKeyboard:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtFirstName resignFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtLastName resignFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtGender resignFirstResponder];
}
else if([self.txtBirthdate isFirstResponder])
{
[self.txtBirthdate resignFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtEmail resignFirstResponder];
}
}
-(void) gotoPreviousField:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtEmail becomeFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtFirstName becomeFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtLastName becomeFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtBirthdate becomeFirstResponder];
}
}
-(void)gotoNextField:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtLastName becomeFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtGender becomeFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtBirthdate becomeFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtFirstName becomeFirstResponder];
}
}
-(void)setBirth
{
dateSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleDefault];
CGRect pickerFrame=CGRectMake(0, 44, 0, 0);
UIDatePicker *birthDayPicker=[[UIDatePicker alloc] initWithFrame:pickerFrame];
[birthDayPicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:birthDayPicker];
UIToolbar *birthDayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[birthDayToolbar setBarStyle:UIBarStyleBlackTranslucent];
[birthDayToolbar sizeToFit];
UIBarButtonItem *btnPrevious = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoPreviousBirthdate:)];
UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextBirthdate:)];
UIBarButtonItem *btnExtraSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *btnDone=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet:)];
[birthDayToolbar setItems:[[NSArray alloc] initWithObjects:btnPrevious, btnNext, btnExtraSpace, btnDone, nil]];
[dateSheet addSubview:birthDayToolbar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissDateSet
{
NSArray *listOfViews = [dateSheet subviews];
for(UIView *subView in listOfViews)
{
if([subView isKindOfClass:[UIDatePicker class]])
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
self.txtBirthdate.text=[dateFormatter stringFromDate:[(UIDatePicker*)subView date]];
}
}
}
-(void)gotoPreviousBirthdate
{
[self dismissDateSet];
[self.txtGender becomeFirstResponder];
}
-(void)gotoNextBirthdate
{
[self dismissDateSet];
[self.txtEmail becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CreateCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *textField = [[UITextField alloc] init];
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.clearButtonMode=UITextFieldViewModeWhileEditing;
textField.delegate=self;
CGRect aRect=CGRectMake(10, 10.f, CGRectGetWidth(cell.bounds)-40.f, 30.f );
textField.frame = aRect;
if(indexPath.row==0)
{
textField.placeholder = #"First name";
self.txtFirstName=textField;
}
else if(indexPath.row==1)
{
textField.placeholder = #"Last name";
self.txtLastName=textField;
}
else if(indexPath.row==2)
{
textField.placeholder = #"Gender";
self.txtGender=textField;
}
else if(indexPath.row==3)
{
textField.placeholder = #"Date of birth";
self.txtBirthdate=textField;
}
else
{
textField.placeholder = #"Email";
self.txtEmail=textField;
}
[cell.contentView addSubview:textField];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
textField.inputAccessoryView=keyboardToolbar;
return cell;
}
#end
If i press the Previous button from the date picker toolbar i get this exception:
[CreateAccountViewController gotoPreviousBirthdate:]: unrecognized selector sent to instance
If i press the Next button from the date picker toolbar i get this exception:
[CreateAccountViewController gotoNextBirthdate:]: unrecognized selector sent to instance
If i press the Done button from the date picker toolbar i get this exception:
[CreateAccountViewController dismissDateSet:]: unrecognized selector sent to instance
It seems that i am making the same mistake 3 times but i can't quite figure out what is that.
Thanks
Your setting the target selector to be:
#selector(gotoPreviousBirthdate:)
But you declare it as:
-(void)gotoPreviousBirthdate;
Remove the : in the selector as #selector(gotoPreviousBirthdate) since you're not taking an argument, or change the declaration to be -(void)gotoPreviousBirthday:(id)sender.
Then repeat for the other two.
You are calling the methods as if they are class methods, but they are declared as instance methods in your interface. Also the invocation of the selector is incorrect.
Create an instance of the class first:
CreateAccountViewController *vc = [[CreateAccountViewController alloc] init]; //Something like this
Then you can call the instance methods:
[vc gotoPreviousBirthday]; //Note no trailing colon!
Or, if you declared the method as
+(void)gotoPreviousBirthdate;
note the + in the signature - then you could call the method as your code shows.
This is a pretty fundamental concept in Objective-C so I would recommend you read the documentation first. Apple Objective-C Primer

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;
}
}

Multiple UIPickers to show in action sheet

in one of my view controllers I want to use multiple pickers.
Header file:
#interface MyTableController : TTTableViewController <UIActionSheetDelegate, UIPickerViewDataSource, UIPickerViewDelegate>{
IBOutlet UIPickerView *picker1;
IBOutlet UIPickerView *picker2;
NSMutableArray *list1;
NSMutableArray *list2;
}
#property(nonatomic,retain) UIPickerView *picker1, *picker2;
-(IBAction)togglePickerView1;
-(IBAction)togglePickerView2;
#end
Implementation file:
#implementation MyTableController
#synthesize picker1, picker2;
int row_index1 = 0;
int row_index2 = 0;
- (void)locationPicker:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
if([pickerView isEqual: picker1]){
row_index1 = row;
}
if([pickerView isEqual: picker2]){
row_index2 = row;
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if([pickerView isEqual: picker1]){
return 1;
}
if([pickerView isEqual: picker2]){
return 1;
}
return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if([pickerView isEqual: picker1]){
return [list1 count];
}
if([pickerView isEqual: picker2]){
return [list2 count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{
return [list objectAtIndex:row];
if([pickerView isEqual: picker1]){
return [list1 objectAtIndex:row];
}
if([pickerView isEqual: picker2]){
return [list2 objectAtIndex:row];
}
return nil;
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
if (actionSheet.tag == 111) {
picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
picker1.showsSelectionIndicator = YES;
picker1.dataSource = self;
picker1.delegate = self;
//Add picker to action sheet
[actionSheet addSubview:picker1];
[picker1 release];
}else if(actionSheet.tag == 222){
picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
picker2.showsSelectionIndicator = YES;
picker2.dataSource = self;
picker2.delegate = self;
//Add picker to action sheet
[actionSheet addSubview:picker2];
[picker2 release];
}
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}
-(IBAction)togglePickerView1{
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(#"FLT", nil) delegate:self cancelButtonTitle:NSLocalizedString(#"CANCEL", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(#"PICK", nil), nil];
[asheet setTag:111];
[asheet showInView:[self.view superview]]; //note: in most cases this would be just self.view, but because I was doing this in a tabBar Application, I use the superview.
[asheet setFrame:CGRectMake(0, 117, 320, 383)];
[asheet release];
}
-(IBAction)togglePickerView2{
//...
[asheet setTag:222];
//...
}
- (void)loadView {
[super loadView];
}
-(void)viewDidLoad{
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"Button1" style:UIBarButtonItemStyleBordered target:self action:#selector(togglePickerView1)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"Button2" style:UIBarButtonItemStyleBordered target:self action:#selector(togglePickerView2)];
NSArray *myToolbarItems = [[NSArray alloc] initWithObjects: item1, item2, nil];
[self setToolbarItems: myToolbarItems];
[myToolbarItems release];
list1 = [[NSMutableArray alloc] init];
[list1 addObject:#"--"];
[list1 addObject:#"Test1"];
list2 = [[NSMutableArray alloc] init];
[list2 addObject:#"--"];
[list2 addObject:#"Test2"];
}
#end
My problem is that no matter which button I hit, it is always the picker1 that is triggered. Any ideas where the problem is?
You are successfully creating two different pickers, and showing the correct one each time.
The problem is, each picker has the same data in it.
The first line in your data source titleForRow... method is this:
return [list objectAtIndex:row];
This ends execution of your data source method by returning a value, so both pickers will always show the same data, regardless of the rest of your code. list isn't declared anywhere in your code above so I'm not sure what you are actually seeing on the screen.
I have built a sample project using your code above and confirmed that this is the issue. Removing that line gives you two different pickers, with the different content in each one.