Get value from UIPickerView - objective-c
I can't get initial value from UIPickerView.
Here is some code:
.......
#define kMaximumPlayers 15
......
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.totalPlayersPossible = [NSMutableArray array];
for (int x = 2; x < kMaximumPlayers; x++)
{
[_totalPlayersPossible addObject:[NSNumber numberWithInt:x]];
}
}
return self;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%#", [self.totalPlayersPossible objectAtIndex:row]];
- (void)viewDidLoad
{
[super viewDidLoad];
[self.pickverView selectRow:0 inComponent:0 animated:YES];
}
}
When I run the app first row of UIPickerView is selected. The problem is that I can't get the value of that row:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//here I get the value of selected row
[self setNumberOfSelectedPlayers:[[self.totalPlayersPossible objectAtIndex:row]intValue]];
}
The value of setNumberOfSelectedPlayers is 0.
What I miss here ?
As Novarg says, the didSelectRow: message is not called on load. You can call the pickerView: titleForRow: inComponent message directly, though, to obtain the title of the currently selected item at any time. Assuming the title you want is in the first "reel" (component) of the picker view:
NSString *initialTitle = [self pickerView:self.pickerView
titleForRow:[self pickerView selectedRowInComponent:0]
forComponent:0
];
This assumes self serves as the UIPickerViewDelegate for the pickerView, of course.
The problem is that the first time you see that UIPickerView the method
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
does not get called because you did not select it. That method is only called when you effectively select a row(scroll and select one of them).
Hope it helps
Related
How can i populate UIPickerView from 1.0 to 50.0 in most elegant way?
I tried populating UIPickerView using array = [[NSArray alloc] initWithObjects=#"1",#"1.2"....#"50.0",nil]. But the issue is that the view crashes automatically once i click it in a TabBarController. Also, this involves a lot of work. What's the most elegant way to achieve this? Update: Here's the code which crashes the view with an error - "lldb". Can you tell me the issue here? - (void)viewDidLoad { kms = [[NSArray alloc] initWithObjects:#"1",#"1.1",#"1.2",#"1.3",#"1.4",#"1.5",#"1.6",#"1.7",#"1.8",#"1.9",#"2.0",#"2.1",#"2.2",#"2.3",#"2.4",#"2.5",#"2.6",#"2.7",#"2.8",#"2.9",#"3.0",#"3.1",#"3.2",#"3.3",#"3.4",#"3.5",#"3.6",#"3.7",#"3.8",#"3.9",#"4.0",#"4.1",#"4.2",#"4.3",#"4.4",#"4.5",#"4.6",#"4.7",#"4.8",#"4.9",#"5.0",#"5.1",#"5.2",#"5.3",#"5.4",#"5.5",#"5.6",#"5.7",#"5.8",#"5.9",#"6.0",#"6.1",#"6.2",#"6.3",#"6.4",#"6.5",#"6.6",#"6.7",#"6.8",#"6.9",#"7.0",#"7.1",#"7.2",#"7.3",#"7.4",#"7.5",#"7.6",#"7.7",#"7.8",#"7.9",#"8.0",#"8.1",#"8.2",#"8.3",#"8.4",#"8.5",#"8.6",#"8.7",#"8.8",#"8.9",#"9.0",#"9.1",#"9.2",#"9.3",#"9.4",#"9.5",#"9.6",#"9.7",#"9.8",#"9.9",#"10.0",#"10.1",#"10.2",#"10.3",#"10.4",#"10.5",#"10.6",#"10.7",#"10.8",#"10.9",#"11.0",#"11.1",#"11.2",#"11.3",#"11.4",#"11.5",#"11.6",#"11.7",#"11.8",#"11.9",#"12.0",#"12.1",#"12.2",#"12.3",#"12.4",#"12.5",#"12.6",#"12.7",#"12.8",#"12.9",#"13.0",#"13.1",#"13.2",#"13.3",#"13.4",#"13.5",#"13.6",#"13.7",#"13.8",#"13.9",#"14.0",#"14.1",#"14.2",#"14.3",#"14.4",#"14.5",#"14.6",#"14.7",#"14.8",#"14.9",#"15.0",#"15.1",#"15.2",#"15.3",#"15.4",#"15.5",#"15.6",#"15.7",#"15.8",#"15.9",#"16.0",#"16.1",#"16.2",#"16.3",#"16.4",#"16.5",#"16.6",#"16.7",#"16.8",#"16.9",#"17.0",#"17.1",#"17.2",#"17.2",#"17.3",#"17.4",#"17.5",#"17.6",#"17.7",#"17.8",#"17.9",#"18.0",#"18.1",#"18.2",#"18.3",#"18.4",#"18.5",#"18.6",#"18.7",#"18.8",#"18.9",#"19.0",#"19.1",#"19.2",#"19.3",#"19.4",#"19.5",#"19.6",#"19.7",#"19.8",#"19.9",#"20.0",#"20.1",#"20.2",#"20.3",#"20.4",#"20.5",#"20.6",#"20.7",#"20.8",#"20.9",#"30.0",#"31.1",#"31.2",#"31.3",#"31.4",#"31.5",#"31.6",#"31.7",#"31.8",#"31.9",#"32.0",#"32.1",#"32.2",#"32.3",#"32.4",#"32.5",#"32.6",#"32.7",#"32.8",#"32.9",#"33.0",#"33.1",#"33.2",#"33.3",#"33.4",#"33.5",#"33.6",#"33.7",#"33.8",#"33.9",#"34.0",#"34.1",#"34.2",#"34.3",#"34.4",#"34.5",#"34.6",#"34.7",#"34.8",#"34.9",#"35.0",#"35.1",#"35.2",#"35.3","#35.4",#"35.5",#"35.6",#"35.7",#"35.8",#"35.9",#"36.0",#"36.1",#"36.2",#"36.3",#"36.4,"#"36.5",#"36.6",#"36.7",#"36.8",#"36.9",#"37.0",#"37.1",#"37.2",#"37.3",#"37.4",#"37.5",#"37.6",#"37.7",#"37.8",#"37.9",#"38.0",#"38.1",#"38.2",#"38.3",#"38.4",#"38.5",#"38.6",#"38.7",#"38.8",#"38.9",#"40.0",#"40.1",#"40.2",#"40.3",#"40.4",#"40.5",#"40.6",#"40.7",#"40.8",#"40.9",#"41.0",#"41.1",#"41.2",#"41.3",#"41.4",#"41.5",#"41.6",#"41.7",#"41.8",#"41.9",#"42.0",#"42.1",#"42.2",#"42.3",#"42.4",#"42.5",#"42.6",#"42.7",#"42.8",#"42.9",#"43.0",#"43.1",#"43.2",#"43.3",#"43.4",#"43.5",#"43.6",#"43.7",#"43.8",#"43.9",#"44.0",#"44.1",#"44.2",#"44.3",#"44.4",#"44.5",#"44.6",#"44.7",#"44.8",#"44.9",#"45.0",#"45.1",#"45.2",#"45.3",#"45.4",#"45.5",#"45.6",#"45.7",#"45.8",#"45.9",#"46.0",#"46.1",#"46.2",#"46.3",#"46.4",#"46.5",#"46.6",#"46.7",#"46.8",#"46.9",#"47.0",#"47.1",#"47.2",#"47.3",#"47.4",#"47.5",#"47.6",#"47.7",#"47.8",#"47.9",#"48.0",#"48.1",#"48.2",#"48.3",#"48.4",#"48.5",#"48.6",#"48.7",#"48.8",#"48.9",#"49.0",#"49.1",#"49.2",#"49.3",#"49.4",#"49.5",#"49.6",#"49.7",#"49.8",#"49.9",#"50.0",nil]; [super viewDidLoad]; // Do any additional setup after loading the view. } - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return kms.count; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [kms objectAtIndex:row]; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { [kms objectAtIndex:row]; }
If by "elegant", you mean a simple way without populating an array then do this: - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return 500 - 10 + 1; // 1.0 to 50.0 is 491 total rows } - (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { // row 0 becomes 1.0, row 1 becomes 1.1, etc. float num = row / 10.0 + 1.0; return [_formatter stringFromNumber:#(num)]; } - (Void)viewDidLoad { [super viewDidLoad]; // _formatter is an NSNumberFormatter * ivar _formatter = [[NSNumberFormatter alloc] init]; [_formatter setNumberStyle: NSNumberFormatterDecimalStyle]; [_formatter setMaximumFractionDigits:1]; } The number formatter is used to ensure the decimals are formatted properly for the user's locale.
Set your class to be the delegate and data source of the UIPickerView, then implements these methods: #synthesize pickerView, entries; - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [entries count]; } - (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [entries objectAtIndex: row]; } #end Where entries is the array containing all the formatted numbers.If you also want to know why your app crashes, post the code. EDIT About populating the view: in the view did load method start to populated the array.Don't worry about pushing the values into the picker view, they will be loaded automatically: entires=[NSMutableArray new]; for(float i=1.0; i<=50.0; i+= 0.1) { NSString* pushString=[NSString stringWithFormat: #"%f",i]; [entries addObject: pushString]; } This makes the picker view display 1.0 , 1.1, 1.2, 1.3, etc...
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]; }
UIPickerView undeclared?
I got a slight problem, I'm getting a error saying "'UIPickerView' undeclared on line 1 of the below code. I copied right out of the book that I have and I'm not sure whats wrong? could I get some help? it would be greatly appreciated, thanks :) this is the entire .m file code. #import "InstatwitViewController.h" #implementation InstatwitViewController - (void)viewDidLoad { [super viewDidLoad]; activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil]; feelings = [[NSArray alloc] initWithObjects:#"awesome", #"sad", #"happy", #"ambivalent", #"nauseous", #"psyched", #"confused", #"hopeful", #"anxious", nil]; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *) pickerView { return 2; } - (NSInteger)PickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent : (NSInteger)component { if (component == 0) { return [activities count]; } else { return [feelings count]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { switch (component) { case 0: return [activities objectAtIndex:row]; case 1: return [feelings objectAtIndex:row]; } return nil; } } - (void)dealloc { [activities release]; [feelings release]; [super dealloc]; } #end
Where did you copy it? It should be in the #implementation ... #end section of the .m file of the class you are implementing. It should not be in the #interface ... #end part, which is typically in a .h file, or elsewhere, i.e. outside the #implementation ... #end section. Please show a little more of your code. Edit This: - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { lacks a closing } before - (NSString *)pickerView: .... In other words, change it thus: - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
UIPickerView won't show
I have been trying for 2 days now to get a UIPickerView to show and I am at a loss. I have implemented all the methods to connect a data source (NSArray) and have no idea why I can't get it to show. The assignment is now due tonight and the professor is less than useless, I've given up asking her. This is the .m file of the subview containing the UIPickerView. All I need is to figure out why the picker won't display! Help appreciated I'm exasperated.... #import "SpotPicker.h" #implementation SpotPicker #synthesize spotPicker; NSArray *pickerLocations; NSString *selectedLocation; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal; pickerLocations = [[NSArray alloc]initWithObjects:#"Grand Canyon", #"Mt Rushmore", #"Statue of Liberty", #"Empire State Building", #"Hollywood Sign", #"Lincoln Memorial", #"Space Needle", nil]; } - (void)viewDidUnload { [self setSpotPicker:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (IBAction)dismissPicker:(id)sender { [self dismissModalViewControllerAnimated:YES]; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { return [pickerLocations count]; } - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [pickerLocations objectAtIndex:row]; } - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { //NSLog(#"Selected Color: %#. Index of selected color: %i", [pickerLocations objectAtIndex:row], row); selectedLocation = [pickerLocations objectAtIndex:row]; } -(NSString *) getLocation { return selectedLocation; } #end Here's the header file to just to make sure I haven't declared something incorrectly or something stupid #import <UIKit/UIKit.h> #interface SpotPicker : UIViewController - (IBAction)dismissPicker:(id)sender; -(NSString *) getLocation; #property (weak, nonatomic) IBOutlet UIPickerView *spotPicker; #end
You need to declare that your SpotPicker implements the UIPickerViewDelegate and UIPickerViewDataSource protocols: #interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> and set your UIPickerView's delegate to be your SpotPicker. You could either do this in IB, or programatically: - (void)viewDidLoad { ... self.spotPicker.delegate = self; self.spotPicker.dataSource = self; }
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 ..