How to set a default Value of a UIPickerView
Solution 1
TL:DR version:
//Objective-C
[self.picker selectRow:2 inComponent:0 animated:YES];
//Swift
picker.selectRow(2, inComponent:0, animated:true)
Either you didn't set your picker to select the row (which you say you seem to have done but anyhow):
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
OR you didn't use the the following method to get the selected item from your picker
- (NSInteger)selectedRowInComponent:(NSInteger)component
This will get the selected row as Integer from your picker and do as you please with it. This should do the trick for yah. Good luck.
Anyhow read the ref: https://developer.apple.com/documentation/uikit/uipickerview
EDIT:
An example of manually setting and getting of a selected row in a UIPickerView:
the .h file:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *picker;
NSMutableArray *source;
}
@property (nonatomic,retain) UIPickerView *picker;
@property (nonatomic,retain) NSMutableArray *source;
-(void)pressed;
@end
the .m file:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize picker;
@synthesize source;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor yellowColor];
self.source = [[NSMutableArray alloc] initWithObjects:@"EU", @"USA", @"ASIA", nil];
UIButton *pressme = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 280, 80)];
[pressme setTitle:@"Press me!!!" forState:UIControlStateNormal];
pressme.backgroundColor = [UIColor lightGrayColor];
[pressme addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pressme];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 110, 280, 300)];
self.picker.delegate = self;
self.picker.dataSource = self;
[self.view addSubview:self.picker];
//This is how you manually SET(!!) a selection!
[self.picker selectRow:2 inComponent:0 animated:YES];
}
//logs the current selection of the picker manually
-(void)pressed
{
//This is how you manually GET(!!) a selection
int row = [self.picker selectedRowInComponent:0];
NSLog(@"%@", [source objectAtIndex:row]);
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [source count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [source objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// NSLog(@"%@", [source objectAtIndex:row]);
}
@end
EDIT for Swift solution (Source: Dan Beaulieu's answer)
Define an Outlet:
@IBOutlet weak var pickerView: UIPickerView! // for example
Then in your viewWillAppear or your viewDidLoad, for example, you can use the following:
pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
If you inspect the Swift 2.0 framework you'll see .selectRow defined as:
func selectRow(row: Int, inComponent component: Int, animated: Bool)
option clicking .selectRow in Xcode displays the following:
Solution 2
This is How to set a default Value of a UIPickerView
[self.picker selectRow:4 inComponent:0 animated:YES];
Solution 3
Swift solution:
Define an Outlet:
@IBOutlet weak var pickerView: UIPickerView! // for example
Then in your viewWillAppear or your viewDidLoad, for example, you can use the following:
pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
If you inspect the Swift 2.0 framework you'll see .selectRow
defined as:
func selectRow(row: Int, inComponent component: Int, animated: Bool)
option clicking .selectRow in Xcode displays the following:
Solution 4
I too had this problem. But apparently there is an issue of the order of method calls. You must call:
[self.picker selectRow:2 inComponent:0 animated:YES];
after calling
[self.view addSubview:self.picker];
Solution 5
You have to send
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
to the picker view before it appears. The documentation states that the method selectedRowInComp... will give -1, thus it is possible that the picker view is in a state with no selected row. It turns out to be in that state when created.
Chris
Updated on July 05, 2022Comments
-
Chris almost 2 years
I have a problem with my UIPickerView. I have 3 values in it EU AP and NA. When I start the app EU seems to be selected but when I make a
NSLog(@"%@", [regions objectAtIndex:row]);
I only get back(null)
, now when I touch the UIPickerView the EU value is selected and I get"EU"
back from a NSLog.My question is:
How can I define a default value which is selected (not only the label) when the user only starts the app and touches nothing.
Edit: Here is my code to get the selected item:
#pragma mark - #pragma mark PickerView DataSource - (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [regions count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [regions objectAtIndex:row]; } #pragma mark - #pragma mark PickerView Delegate -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { selectedRegion = [[NSString alloc] initWithFormat: @"%@", [regions objectAtIndex:row]]; NSLog(@"%@", selectedRegion); }
-
Chris almost 12 yearswhere should i send this to the picker? sry i'm a beginner at Xcode :(
-
Totumus Maximus almost 12 yearsWell the delegate only activates if you touch the picker and let it become active. If you want default values outside the pickerfocus you can't rely on those delegates. You need to set and get the defaults manually. The way you want it to work (with those delegates) won't work.
-
Chris almost 12 yearsmhmm i tried to implent it into my project but it did not work :( do you have any simple example with that? sry i am very new to objective C programming :/
-
Totumus Maximus almost 12 yearsSure I'll add one in a moment or 2
-
Totumus Maximus almost 12 yearsHere is a quick example on setting and getting of the picker selection. Of course you will need to use it in your own way still but the basics should be clear. This will help you. Good luck.
-
Orkhan Alikhanov almost 7 yearsPicture shows
Availability iOS 8.1+
while it isiOS 2.0+
. Weird (: -
Totumus Maximus almost 7 years@OrkhanAlikhanov the image was made in 2015. There were probably some changes on the apple document page. They seem to have removed the availability section.
-
ekashking almost 5 yearsHowever, if your picker is attached to
field.inputView = picker
then, after, you have to call it with a slight delay like this:dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.picker selectRow:2 inComponent:0 animated:YES]; });
-
Dave over 3 yearsYou can't set the value before the picker view appears because it isn't in the view hierarchy yet. Invoking this method on a pickerview with no superview will generally result in an exception being thrown which crashes your app.