how to solve uipickerview - objective-c

I have a problem with my pickerview where by clicking on the textfield it shows an empty uipickerview. I have 3 textfield where by clicking on each textfield there will be different array for each one.
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
tbMode.inputView = pickerView;
tbWalkDist.inputView = pickerView;
tbRouteOpt.inputView = pickerView;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField*)textField;
{
modeArray = [[NSMutableArray alloc] init];
if(textField.tag==1){
[modeArray addObject:#" A "];
[modeArray addObject:#" B "];
pickerView.hidden = NO;
}
else{
if(textField.tag==2){
[modeArray addObject:#" 300 "];
[modeArray addObject:#" 400 "];
[modeArray addObject:#" 500 "];
pickerView.hidden = NO;
}
else{
if(textField.tag==3){
[modeArray addObject:#" cheapest "];
[modeArray addObject:#" fastest "];
pickerView.hidden = NO;
}
else{
pickerView.hidden=YES;
}
}
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
tbMode.text = [modeArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [modeArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [modeArray objectAtIndex:row];
}
pls help! where did i got wrong? is it possible that this method -(void)textFieldDidBeginEditing:(UITextField*)textField; is not called? thanks
edit* i miss out these 3 line of line which is for each textfield which use to call that method in viewDidLoad
tbMode.delegate = self;
tbWalkDist.delegate = self;
tbRouteOpt.delegate = self;
But I'm still getting an empty picker view. how can i add these NSMutableArray into the the picker view
*add in [pickerView reloadAllComponents]; the picker will display the values :)

I don't see here where you initialize the pickerView ivar. Can I assume it is created in xib?
If so, make sure you have connected the iBOutlet and set up its delegate and datasource property to be the "file owner" or view controller as it were.
If you create the picker in the init method, post that code so we can see if those properties are set correctly.
You also must have set your controller as the delegate for the uiTextField.
Additionally, if they are not implemented elsewhere, you are missing these required protocols:
– pickerView:rowHeightForComponent:
– pickerView:widthForComponent:

As for your output problem, you need to keep track of the uiTextField that activated. Add an ivar,
uiTextField *activeField;
set the variable to the appropriate uiTextField object in the textfield delegate method. Then replace,
tbMode.text = [modeArray objectAtIndex:row];
with,
activeField.txt = [modeArray objectAtIndex:row];
Set the ivar like so:
-(void)textFieldDidBeginEditing:(UITextField*)textField
{
activeField = textField;
// all the if else logic here
}

Try [pickerView reloadAllComponents] after setting the new data set.

Related

2 UI picker views in one view interfering with each UI Picker's text view

I'm building an app with two UI textFields. When the UI textfields are touched a UI Picker comes up for the user to select a value to stay in the UI textField. When I test the app and touch one UI TextView and scroll the UI Picker, both UI textFields change values. Meaning, each UI Text Field is being affected from each UI Picker. Anyone know why? Thank you.
#interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
#end
#implementation ViewController
#synthesize ageTextField;
#synthesize relationshipTextField;
#synthesize resultLabel;
#synthesize calculateButton;
#synthesize agePickerView;
#synthesize relationshipPickerView;
#synthesize ageArray;
#synthesize relationshipArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//age pickerView
self.agePickerView = [[UIPickerView alloc]init];
self.agePickerView.delegate = self;
self.agePickerView.dataSource = self;
self.agePickerView.showsSelectionIndicator = YES;
self.ageTextField.inputView = self.agePickerView;
self.ageArray = #[ #"1", #"2", #"3", #"4", #"5", #"5", #"6", #"7", #"8", #"9", #"10", #"11", #"12"
, #"13", #"14", #"15", #"16", #"17", #"18", #"19", #"20", #"21", #"22", #"23", #"24", #"25", #"26", #"27", #"28", #"29", #"30", #"31", #"32", #"33", #"34", #"35", #"36", #"37", #"38", #"39", #"40", #"41", #"42", #"43", #"44", #"45", #"46", #"47", #"48", #"49", #"50", #"51", #"52", #"53", #"54", #"55", #"56", #"57", #"58", #"59", #"60", #"61", #"62", #"63", #"64", #"65", #"66", #"67", #"68", #"69", #"70", #"71", #"72", #"73", #"74", #"75", #"76", #"77", #"78", #"79", #"80", #"81", #"82", #"83", #"84", #"85", #"86", #"87", #"88", #"89", #"90", #"91", #"92", #"93", #"94", #"95", #"96", #"97", #"98", #"99", #"100" ];
[self pickerView:self.agePickerView
didSelectRow:0
inComponent:0];
//relationship pickerView
self.relationshipPickerView = [[UIPickerView alloc]init];
self.relationshipPickerView.delegate = self;
self.relationshipPickerView.dataSource = self;
self.relationshipPickerView.showsSelectionIndicator = YES;
self.relationshipTextField.inputView = self.relationshipPickerView;
self.relationshipArray = #[ #"1", #"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13", #"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25", #"26",#"27",#"28",#"29",#"30",#"31",#"32",#"33",#"34",#"35",#"36",#"37", #"38",#"39",#"40",#"41",#"42",#"43",#"44",#"45",#"46",#"47",#"48",#"49",#"50", #"51", #"52", #"53", #"54", #"55", #"56", #"57", #"58", #"59", #"60", #"61", #"62", #"63", #"64", #"65", #"66", #"67", #"68", #"69", #"70", #"71", #"72", #"73", #"74", #"75", #"76", #"77", #"78", #"79", #"80", #"81", #"82", #"83", #"84", #"85", #"86", #"87", #"88", #"89", #"90", #"91", #"92", #"93", #"94", #"95", #"96", #"97", #"98", #"99", #"100"];
[self pickerView:self.relationshipPickerView
didSelectRow:0
inComponent:0];
}
-(int) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.ageArray count];
return [self.relationshipArray count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.ageArray objectAtIndex:row];
return [self.relationshipArray objectAtIndex:row];
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component{
self.ageTextField.text = [self.ageArray objectAtIndex:row];
self.relationshipTextField.text = [self.relationshipArray objectAtIndex:row];
}
- (void)viewDidUnload
{
[self setAgeTextField:nil];
[self setRelationshipTextField:nil];
[self setResultLabel:nil];
[self setCalculateButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.ageArray count];
return [self.relationshipArray count];
}
That's not how C (or Objective-C) works. It will always only execute the first line.
You need to conditionalize the return value based on which UIPickerView is asking for the information:
if (pickerView == self.agePickerView) {
return [self.ageArray count];
} else {
return [self.relationshipArray count];
}
And then apply that same pattern to all the datasource methods.

How to get more than one Array in a picker view

Im really close on this one...For the purposes of this i have two text boxes. when pressed they invoke a ui picker.
text box one returns values 1,2,3,4
text box two returns values A, B, C, D
The problem is that when the picker is presented for text box two it shows the values from text box one, although will select the appropriate row from text box two. Could someone check over my code and tell me where im making an error, thanks.
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate,UITextFieldDelegate>
{
//decalre picker
UIPickerView *select;
//declare NSArrray
NSArray *arrStatus;
NSArray *arrStatus2;
}
#property (strong, nonatomic) IBOutlet UITextField *text1;
- (IBAction)Value:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *text2;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize text2;
#synthesize text1;
- (void)viewDidLoad
{
select = [[UIPickerView alloc] initWithFrame:CGRectZero];
select.delegate = self;
select.dataSource = self;
[select setShowsSelectionIndicator:YES];
text1.inputView = select;
text2.inputView = select;
arrStatus = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
arrStatus2 = [[NSArray alloc] initWithObjects:#"A",#"B",#"C",#"D",nil];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return arrStatus.count;
return arrStatus2.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//set item per row
return [arrStatus objectAtIndex:row];
return [arrStatus2 objectAtIndex:row];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setText1:nil];
[self setText2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)Value:(id)sender {
NSInteger selectedRow = [select selectedRowInComponent:0];
if([text1 isFirstResponder])
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];
if ([text2 isFirstResponder])
text2.text = [arrStatus2 objectAtIndex: selectedRow];
[text2 resignFirstResponder];
}
#end
So to sum up im trying to get, 1 picker view, with 1 colum, 2 arrays, 2 text boxes. Ive tried creating another picker view and that didnt work either
The return statement exits the method. Therefore , if you have 2 return statements , it will never get to the second one. You should modify these methods as such:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if([text1 isFirstResponder])
return arrStatus.count;
else
return arrStatus2.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if([text1 isFirstResponder])
return [arrStatus objectAtIndex:row];
else
return [arrStatus2 objectAtIndex:row];
}
Hope this helps. Cheers!
This is Code Which is able to show picker view with the given array and textfield will have the first data of the UIPickerView.Please let me know if you have any issue.I did it for one textfield for next textfield you just need to figure out using tag of textfield and set the Text.
-(void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
[pickerView selectRow:1 inComponent:0 animated:NO];
self.text1.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
mlabel.text= [arrayNo objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayNo count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arrayNo objectAtIndex:row];
}

Picker View only selects first object from NSArray in text box

Im trying to learn how to select an object from a picker view to display in a TextBox, six hours in I thought id finally cracked but no. It works, but I can only get the first item in my NSArray to display in my text box.
Select being my picker, arrstatus is my NSArray, text1 is my text box.
Using a button to get my value: under IBAction:
NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex:selectedRow];
[text1 resignFirstResponder];
rest of code:
.h
#interface pick5 : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate,UITextFieldDelegate>
{
UIPickerView *select;
NSArray *arrStatus;
}
.m
UIPickerView *select = [[UIPickerView alloc] initWithFrame:CGRectZero];
select.delegate = self;
select.dataSource = self;
[select setShowsSelectionIndicator:YES];
text1.inputView = select;
arrStatus = [[NSArray alloc] initWithObjects:#"bs88", #"bs60898", #"Memphis", #"California", #"Seattle",#"London",#"Paris", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return arrStatus.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//set item per row
return [arrStatus objectAtIndex:row];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setText1:nil];
[self setResign:nil];
[self setText2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)resign:(id)sender {
NSInteger selectedRow = [select selectedRowInComponent:0];
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];
}
#end
im also getting yellow warning triangles local decalration of "select" hides instance variable
For your warning in yellow color change the following line of code in your .m file.
UIPickerView *select = [[UIPickerView alloc] initWithFrame:CGRectZero];
replace above with below:
select = [[UIPickerView alloc] initWithFrame:CGRectZero];
because you have already declared uipicker* select in .h file.

need help for UIPickerView keyboard

New to objective c and this maybe a simple error. I am trying to dismiss all 3 UITextField keyboard which i have for UIPickerView. Whereby the keyboard won't appear only the picker view would be shown. I did some research but can't figure it out. Here is the code for the .m file and it won't work.
-(void)textFieldDidBeginEditing:(UITextField*)textField;
{
[textField resignFirstResponder];
modeArray = [[NSMutableArray alloc] init];
if(textField.tag==1){
[modeArray addObject:#" bus "];
[modeArray addObject:#" bus/mrt "];
tbMode.inputView = pickerView;
pickerView.hidden = NO;
}
else{
if(textField.tag==2){
[modeArray addObject:#" 300 "];
[modeArray addObject:#" 400 "];
[modeArray addObject:#" 500 "];
tbWalkDist.inputView = pickerView;
pickerView.hidden = NO;
}
else{
if(textField.tag==3){
[modeArray addObject:#" cheapest "];
[modeArray addObject:#" fastest "];
tbRouteOpt.inputView = pickerView;
pickerView.hidden = NO;
}
else{
pickerView.hidden=YES;
}
}
}
}
how should i dismiss the keyboard only showing the picker view? Using this line [textField resignFirstResponder]; can i use to dimiss the keyboard?
You need to delegate all your text fields to the viewController and then use the method;
-(BOOL)textFieldShouldReturn:(UITextField *)textField // called from textfield (keyboard)
{
//... place your [textField resignFirstResponder]; logic here
}
Make sure you add the text view delegate protocol to your viewcontroller .h file
#interface ViewController : UIViewController <UITextViewDelegate>
I don't understand your question clearly, but if you want this will hide the keyboard if you press return key :
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textFieldName resignFirstResponder];
return YES;
}

UIPickerview gives questionmarks back instead of data

EDIT
I have a UIPickerview and I want do fill it up with data from my NSMutualArray. I have tested if something is in my array with NSLog and i get the data back. But it will not fill up my pickerviewer
I have this methods for my pickerviewer.
- (NSString *)pickView:(UIPickerView *)pickview titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [dataArray objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// assuming a single spinning wheel of strings (not split into left/right for example)
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [json count]; // the number of strings that your parser finds and adds to the array
}
To get my data out of my JSON I use these two methods. And i call the start function in my viewdidload.
-(void) getData:(NSData *) data{
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
-(void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
I have also a button which hides and shows my UIPickerviewer.
-(IBAction)setPicker:(id)sender{
if (pickview.hidden == TRUE) {
[pickview setHidden:NO];
[pickview setDelegate:self];
[pickview setDataSource:self];
[pickview reloadAllComponents];
}else {
[pickview setHidden:YES];
[pickview setDelegate:self];
[pickview setDataSource:self];
[pickview reloadAllComponents];
}
}
I use this method for filling up my array.
-(void) fillArray{
dataArray = [[NSMutableArray alloc] init];
for (int i=0; i<[json count]; i++) {
NSDictionary *info = [json objectAtIndex:i];
[dataArray addObject:[info objectForKey:#"Use_naam"]];
}
NSLog(#"%#\n",dataArray);
}
And i call it in my viewdidload.
- (void)viewDidLoad
{
[super viewDidLoad];
[self start];
[self fillArray];
// Do any additional setup after loading the view, typically from a nib.
}
Anybody knows what I am doing wrong ?
When you see a number of question marks matching the expected number of rows it's usually the methods that returns the title or view for the rows that are missing.
This can either be because the UIPickerViews delegate is not set (you did that) or because the signature is not correct.
Your signature is off. It needs to be exactly:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
^^^^
Most commonly people forget that you have to set the UIPickerView delegate and datasource.