Reputation: 433
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 ?
Upvotes: 0
Views: 3540
Reputation: 5707
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.
Upvotes: 3
Reputation: 7440
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
Upvotes: 1