How to get more than one Array in a picker view - objective-c

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

Related

Xcode UIPickerView "NSInvalidArgumentExpression"?

I'm trying to implement a UIPickerView in a FlipsideViewController, which is just a settings tab.
Here's the relevant code in the .h file:
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate<UIPickerViewDataSource, UIPickerViewDelegate>
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#property (strong, nonatomic) IBOutlet UIPickerView *Categories;
#property (strong, nonatomic) NSArray *categoriesData;
#property (strong, nonatomic) NSMutableArray* dataArray;
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSInteger)Categories:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component;
- (NSString*)Categories:(UIPickerView *)Categories titleForRow:(NSInteger)row forComponent:(NSInteger)component;
Here's the relevant code in the .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_Categories = [[UIPickerView alloc] initWithFrame:CGRectMake((pickerWidth*4/3)/2-pickerWidth/2, 50.0f, pickerWidth, 200.0f)];
_Categories.showsSelectionIndicator = true;
[_Categories selectRow:2 inComponent:0 animated:YES];
[self.view addSubview: _Categories];
_dataArray = [[NSMutableArray alloc] init];
[_dataArray addObject:#"One"];
[_dataArray addObject:#"Two"];
[_dataArray addObject:#"Three"];
[_dataArray addObject:#"Four"];
[_dataArray addObject:#"Five"];
_categoriesData = _dataArray;
_Categories.dataSource = (id)self;
_Categories.delegate = (id)self;
}
- (int)numberOfComponentsInPickerView:(UIPickerView *)Categories
{
return 1;
}
// The number of rows of data
- (NSInteger)Categories:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component
{
return _categoriesData.count;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)Categories:(UIPickerView *)Categories titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_categoriesData objectAtIndex:row];
}
Sorry if that's a little too much to read. The point is that in the final portion, when - (NSInteger)Categories:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component is run, it returns a "NSInvalidArgumentException". I tried reinitializing the dataArray and categoriesData in each method, to no avail. I'm not sure what I should do... please help. Thanks!
EDIT:
Found the error. It was in the method call... instead of specifying which UIPickerView I was trying to access, I instead should have just put in a generic "UIPickerView". So, it should've looked like this:
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{}
and not like this:
- (NSString*)pickerView:(UIPickerView *)Categories titleForRow:(NSInteger)row forComponent:(NSInteger)component
- (NSInteger)pickerView:(UIPickerView *)Categories numberOfRowsInComponent:(NSInteger)component{}
Hope this helps anyone finding a similar problem.
It looks like categoriesData is not assigning the value properly. Try to change this line below and check:-
self.categoriesData = _dataArray;

Adding a UI Picker to a UITextView

I'm trying to add a UIPicker to a UITextView when it is selected to choose an age. I found a previous thread and trued to implement the code but I can't get it to work properly. When I build the program the picker shows up, but there are no values in it. Any ideas as to what I'm doing wrong? Also, I'm getting two yellow errors here saying "Assigning to 'id' from incompatible type 'ViewController *const __strong" here:
pickerView.delegate = self;
pickerView.dataSource = self;
Thank you.
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *ageTextField;
#end
#implementation ViewController
#synthesize ageTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPickerView *pickerView =[[UIPickerView alloc]init];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator=YES;
ageTextField.inputView=pickerView;
}
- (void)pickerView:(UIPickerView *)PickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
NSMutableArray *ageArray = [[NSMutableArray alloc]init];
[ageArray addObject:#"1"];
[ageArray addObject:#"2"];
[ageArray addObject:#"3"];
[ageArray addObject:#"4"];
ageTextField.text=[ageArray objectAtIndex:row];
}
#interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) UIPickerView *pickerView;
#property (strong, nonatomic) NSArray *pickerElements;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
self.ageTextField.inputView = self.pickerView;
self.pickerElements = #[#"text1", #"text2", #"text3", #"text4"];
[self pickerView:self.pickerView
didSelectRow:0
inComponent:0];
}
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.pickerElements count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerElements objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.ageTextField.text = [self.pickerElements objectAtIndex:row];
}
#end

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.

Use of undeclared identifier

Am trying to configure a two component picker, and am getting some errors which I have commented out:
Implementation file:
#import "BIDDoubleComponentPickerViewController.h"
#implementation BIDDoubleComponentPickerViewController
#synthesize doublePicker;
#synthesize fillingTypes;
#synthesize breadTypes;
- (IBAction)buttonPressed:(id)sender
{
NSInteger fillingRow = [doublePicker selectedRowInComponent:kFillingComponent]; // Use of undeclared identifier 'kFillingComponent'
NSInteger breadRow = [doublePicker selectedRowInComponent:kBreadComponent]; // Use of undeclared identifier 'kBreadComponent'
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];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *fillingArray = [[NSArray alloc] initWithObjects:#"Ham", #"Turkey", #"Peanut Butter", #"Tuna Salad", #"Chicken Salad", #"Roast Beef", #"Vegemite", nil];
self.fillingTypes = fillingArray;
NSArray *breadArray = [[NSArray alloc] initWithObjects:#"White", #"Whole Wheat", #"Rye", #"Sourdough Bread", #"Seven Grain", nil];
self.breadTypes = breadArray;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.doublePicker = nil;
self.breadTypes = nil;
self.fillingTypes = nil;
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == kBreadComponent) { // Use of undeclared identifier 'kBreadComponent'
return [self.breadTypes.count]; // Expected identifier
return [self.fillingTypes objectAtIndex:row]; // Use of undeclared identifier 'row'
}
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == kBreadComponent) { // Use of undeclared identifier 'kBreadComponent'
return [self.breadTypes objectAtIndex:row];
return [self.fillingTypes objectAtIndex:row];
}
}
#end
Interface file:
#import <UIKit/UIKit.h>
#interface BIDDoubleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
#property (strong, nonatomic) IBOutlet UIPickerView *doublePicker;
#property (strong, nonatomic) NSArray *fillingTypes;
#property (strong, nonatomic) NSArray *breadTypes;
- (IBAction)buttonPressed:(id)sender;
#end
kBreadComponent and kFillingComponent are not declared anywhere. If they're declared in a header (.h) file, you need to #import it.
Your numberOfRowsInComponent and titleForRow methods are similar, and they are wrong: they have two consequent unconditional return, and this means that the second one will never be executed.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.breadTypes.count;
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == kBreadComponent) {
return [self.fillingTypes objectAtIndex:row];
}
return nil; // Or something, empty string for example. Method requires to return a NSString, so you have to return at least nil;
}
Still you have to find kBreadComponent and kFillingComponent and #import the file with the definitions of them.

UIPickerview in iphone

How to place UIPickerView programmatically in a subView with out using Interface Builder?
Hey! U can make UIPickerView programmatically....
In- viewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,320, 500)];
[self.view addSubview:pickerView];
[pickerView setDelegate:self];
[pickerView release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return ; //give components here
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return ; //give rows here
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return ; // give titles }
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//Do the step here
}
And in your viewController.h class never forgot to do this
#interface viewController : UIViewController <UIPickerViewDelegate>
Hope this may help u.....for more help follow the link ..