68

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


}
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Chris
  • 709
  • 2
  • 6
  • 9

8 Answers8

110

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:

Cœur
  • 37,241
  • 25
  • 195
  • 267
Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • 1
    Well 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. – Totumus Maximus Aug 02 '12 at 13:28
  • mhmm 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 :/ – Chris Aug 02 '12 at 14:09
  • Sure I'll add one in a moment or 2 – Totumus Maximus Aug 02 '12 at 14:37
  • Here 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. – Totumus Maximus Aug 02 '12 at 14:48
  • Picture shows `Availability iOS 8.1+` while [it is `iOS 2.0+`](https://developer.apple.com/documentation/uikit/uipickerview/1614382-selectrow). Weird (: – Orkhan Alikhanov Jul 02 '17 at 01:28
  • @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. – Totumus Maximus Jul 03 '17 at 08:37
18

This is How to set a default Value of a UIPickerView

[self.picker selectRow:4 inComponent:0 animated:YES];
Micha
  • 5,117
  • 8
  • 34
  • 47
Hardik Darji
  • 3,633
  • 1
  • 30
  • 30
11

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:

enter image description here

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
7

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];
user4833236
  • 71
  • 1
  • 1
  • 1
    However, 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]; });` – ekashking Jul 20 '19 at 05:33
  • As an option it is possible to use CATransaction instead of GCD: `[CATransaction begin]; [(UIPickerView *)[textField inputView] selectRow:0 inComponent:0 animated:YES]; [CATransaction commit]; ` – void Feb 20 '23 at 07:39
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.

Per Arve
  • 226
  • 2
  • 7
  • where should i send this to the picker? sry i'm a beginner at Xcode :( – Chris Aug 02 '12 at 12:49
  • 1
    You 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. – Dave Nov 16 '20 at 06:06
1

In normal case, you can do something like this in viewDidLoad method;

[_picker selectRow:1 inComponent:0 animated:YES];

In my case, I'd like to fetch data from api server and display them onto UIPickerView then I want the picker to select the first item by default.

The UIPickerView will look like it selected the first item after it was created, but when you try to get the selected index by using selectedRowInComponent, you will get NSNull. That's because it detected nothing changed by the user (select 0 from 0 ).

Following is my solution (in viewWillAppear, after I fetched the data)

[_picker selectRow:1 inComponent:0 animated:NO];
[_picker selectRow:0 inComponent:0 animated:NO];

Its a bit dirty, but dont worry, the UI rendering in iOS is very fast ;)

Ryan Wu
  • 5,963
  • 2
  • 36
  • 47
1
For example: you populated your UIPickerView with array values, then you wanted 

to select a certain array value in the first load of pickerView like "Arizona". Note that the word "Arizona" is at index 2. This how to do it :) Enjoy coding.

NSArray *countryArray =[NSArray arrayWithObjects:@"Alabama",@"Alaska",@"Arizona",@"Arkansas", nil];
UIPickerView *countryPicker=[[UIPickerView alloc]initWithFrame:self.view.bounds];
countryPicker.delegate=self;
countryPicker.dataSource=self;
[countryPicker selectRow:2 inComponent:0 animated:YES];
[self.view addSubview:countryPicker];
handiansom
  • 783
  • 11
  • 27
0

In case someone is still looking answer to this old question in Swift, here is what worked for me. None of the above answers worked as I had to select the first row in viewDidLoad.

self.pickerView.delegate?.pickerView?(self.pickerView, didSelectRow: 0, inComponent: 0)

Hope this helps.

Shoaib A
  • 63
  • 7