Setting Title for Row in PickerView - ios

I am struggling trying to set my titleForRow in my PickerView. Currently, I have a picker that accesses a user's playlist data. Then I want to transfer it to the picker. However, I know the picker data is correct because of the didSelectRow method. I have been unlucky in trying to display the actual data in the PickerView.
**I have edited my code to what I have now. Currently, Purchased is repeated over and over inside my pickerview. Is there a way to fix it?
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [_roundArray count];
return [_timeArray count];
MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playlists = [myPlaylistsQuery collections];
return [playlists count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == 1) {
lblRound.text = [_roundArray objectAtIndex:row];
}
else if (pickerView.tag == 2) {
lblTime.text = [_timeArray objectAtIndex:row];
}
else if (pickerView.tag ==3) {
MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playlists = [myPlaylistsQuery collections];
for (MPMediaPlaylist *playlist in playlists) {
lblPlaylist.text = [playlist valueForProperty: MPMediaPlaylistPropertyName];
}
}
}
#pragma mark Picker Delegate Methods
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
if (pickerView.tag == 1) // this is otherPickerview
{
title=[_roundArray objectAtIndex:row]; // your logic to get title for otherpickerview
}
else if (pickerView.tag == 2) // this is citysPickerview
{
title=[_timeArray objectAtIndex:row]; // your logic to get title for cityspickerview
}
else if (pickerView.tag == 3)
{
[self showPlaylistData];
MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playlists = [myPlaylistsQuery collections];
for (MPMediaPlaylist *playlist in playlists)
title = [playlist valueForProperty: MPMediaPlaylistPropertyName];
}
return title;
}

The problem is that you have failed to set this class up as your picker view's data source, and you have failed to implement the data source methods. Thus, no data. Consult the documentation on UIPickerViewDataSource. (Or just look at any working example and base your code on that.)

Related

pickerViews crashes

Here is my code:
#synthesize pickerLetter, pickerNumber, pickerSymbol;
- (void)viewDidLoad {
[super viewDidLoad];
letters = [[NSArray alloc]initWithObjects:#"a", #"b", #"c", nil];
numbers = [[NSArray alloc]initWithObjects:#"1", #"2", #"3", nil];
symbols = [[NSArray alloc]initWithObjects:#"+", #"-", #"/", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return letters.count;
} else if (pickerView == pickerNumber){
return numbers.count;
} else {
return symbols.count;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return [letters objectAtIndex:row];
} else if (pickerView == pickerNumber){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}
This is giving me
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
when I use the pickers in the Simulator.
The code that is causing the crash is
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text = [NSString stringWithFormat:#"%# %# %#",[letters objectAtIndex:[pickerLetter selectedRowInComponent:0]],[numbers objectAtIndex:[pickerNumber selectedRowInComponent:1]],[symbols objectAtIndex:[pickerSymbol selectedRowInComponent:2]]];
}
You've narrowed down your issue to this code:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
label.text = [NSString stringWithFormat:#"%# %# %#",[letters objectAtIndex:[pickerLetter selectedRowInComponent:0]],[numbers objectAtIndex:[pickerNumber selectedRowInComponent:1]],[symbols objectAtIndex:[pickerSymbol selectedRowInComponent:2]]];
}
But this line has several calls to objectAtIndex: so it's hard to know the exact issues. Plus this code is hard to read and impossible to debug. Start by splitting up this code as follows:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger letterIndex = [pickerLetter selectedRowInComponent:0];
NSString *letter = letters[letterIndex];
NSInteger numberIndex = [pickerNumber selectedRowInComponent:1];
NSString *number = numbers[numberIndex];
NSInteger symbolIndex = [pickerSymbol selectedRowInComponent:2];
NSString *symbol = symbols[symbolIndex];
label.text = [NSString stringWithFormat:#"%# %# %#", letter, number, symbol];
}
Doing this you will be able to narrow down the real cause of your issue.
As you can see, the problem is that you are referencing the wrong component number from the pickerNumber and pickerSymbol. All three pickers only have 1 component so you need to select component 0 from all three pickers.
The needed code is:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger letterIndex = [pickerLetter selectedRowInComponent:0];
NSString *letter = letters[letterIndex];
NSInteger numberIndex = [pickerNumber selectedRowInComponent:0];
NSString *number = numbers[numberIndex];
NSInteger symbolIndex = [pickerSymbol selectedRowInComponent:0];
NSString *symbol = symbols[symbolIndex];
label.text = [NSString stringWithFormat:#"%# %# %#", letter, number, symbol];
}
Avoid putting more than one or two method calls on a single line of code. It makes the code less readable and much harder to debug if there is a problem.
you are calling a wrong way to implement the method
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == pickerLetter) {
return [letters objectAtIndex:row];
} else if (pickerView == pickerNumber){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}
change it to
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [letters objectAtIndex:row];
} else if (component == 1){
return [numbers objectAtIndex:row];
} else {
return [symbols objectAtIndex:row];
}
}

Return first character of string in NSArray for UIPickerView didSelectRow

I have a UIPickerView which returns an NSArray with 4 strings. When the user scrolls the UIPickerView, a UITextField gets updated with the selected row. I would like to return the first character of each string in the NSArray and update the UITextField with that character.
As you can see from my code, I have tried to achieve this functionality by using self.noiseLevelArray = [noiseArray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];, which I found on a few different stack overflow questions.
Here is the code for the picker:
// Noise picker
self.noisePicker = [[UIPickerView alloc]init];
[noisePicker setDataSource:self];
[noisePicker setDelegate:self];
self.noiseArray = [[NSArray alloc]initWithObjects:#"4: Extreme Noise (Impossible)", #"3: Very Noisy (Loud Shouting)", #"2: Noisy (Shouting)", #"1: Not Noisy (Normally)", nil];
//self.noiseLevelArray = [noiseArray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
// Call noisePicker method
[self noisePickerTextField];
Here is the code for my picker:
// Picker components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [noiseArray count];
//return [noiseLevelArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [noiseArray objectAtIndex:row];
//return [noiseLevelArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *noiseString = [[NSString alloc]initWithFormat:#"%#", [noiseArray objectAtIndex:row]];
//NSString *noiseString = [noiseLevelArray objectAtIndex:0];
if ([self.empNoise1 isFirstResponder]) {
self.empNoise1.text = noiseString;
}
else if ([self.empNoise2 isFirstResponder]) {
self.empNoise2.text = noiseString;
}
...
As you can see, the UITextField gets updated with the full string of whichever row is selected in the UIPickerView. I would like it to still display the full string, but update the UITextField with the first character only (in this case 1, 2, 3, or 4). Any help is much appreciated!
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *noiseString = [[NSString alloc]initWithFormat:#"%#", [[noiseArray objectAtIndex:row] substringToIndex:1]];
//NSString *noiseString = [noiseLevelArray objectAtIndex:0];
if ([self.empNoise1 isFirstResponder]) {
self.empNoise1.text = noiseString;
}
else if ([self.empNoise2 isFirstResponder]) {
self.empNoise2.text = noiseString;
}
}
Try this. It uses the following method:
NSString * firstLetter = [word substringToIndex:1];
Try this.. `
self.empNoise1.text = [noiseString substringToIndex:1];`
Returns a new string containing the characters of the receiver up to, but not including, the one at a given index.
Link

How I can display string format on my Uilabel when selecting data from Uipickerview?

Hi I have one picker view. This picker view from web services came to
loading data. This picker view is 2 part. Picker view 1. part is
Project name. and Picker view 2. part is Project number. I want to
write on my label same picker view is 1. part I want to write on label
"A1 Unitesi", and i want to write on label2 "002" but my label on
write "DevamEdenProjelerObje:"0xada6fe0">why on my label write
"DevamEdenProjelerObje:"0xada6fe0"> ?` Can you help to me ? How i can
on my label write "A1 Unitesi" ?
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return [RaporlarList count];
}
return [RaporlarList count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
eObje = [RaporlarList objectAtIndex:row];
return eObje.ProjeAdii;
}
eObje = [RaporlarList objectAtIndex:row];
return eObje.ProjeNoo;
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0)
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje Adı : %#", [RaporlarList objectAtIndex:row]];
lblProjeAdi.text = [NSString stringWithFormat:#"%#", resultString];
}
else
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje No: %#", [RaporlarList objectAtIndex:row]];
lblProjeNo.text = resultString;
}
}
I didn't know if I understood well but isn't that better?
if (component == 0)
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje Adı : %#", [[RaporlarList objectAtIndex:row] ProjeAdii]];
lblProjeAdi.text = [NSString stringWithFormat:#"%#", resultString];
}
else
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje No: %#", [[RaporlarList objectAtIndex:row] ProjeNoo]];
lblProjeNo.text = resultString;
}

How set UIPickerView in cascade

I'm trying to set two UIPickerViews, both are filled with NSDictionary, this work ok, but I need to the second UIPickerView implement a refresh depends of the selection of the first UIPickerView, understand me?
The first UIPickerView is filled this way:
for (NSDictionary *local in json) {
NSString *nombre = [local objectForKey:#"name"];
NSString *idLocal = [local objectForKey:#"id"];
self.dicLocales = [NSDictionary dictionaryWithObjectsAndKeys: idLocal, #"idLocal", nombre, #"nombreLocal", nil];
[listaLocales addObject:self.dicLocales];
}
And the second UIPickerView is filled this way:
for (NSDictionary *servicio in json) {
NSString *nombre = [servicio objectForKey:#"name"];
NSString *idServicio = [servicio objectForKey:#"id"];
self.dicServicios = [NSDictionary dictionaryWithObjectsAndKeys: idServicio, #"idServicio", nombre, #"nombreServicio", idLocal, #"idLocal", nil];
[listaServicios1 addObject:self.dicServicios];
}
You note variable idLocal, that corresponds to id of the first UIPickerView rows. Then, I need, if in the first UIPickerView is selected row with idLocal 1, in the second UIPickerView is reloaded or refreshing only with rows corresponds to idLocal 1.
Please hope somebody can help me, and sorry for my English :P
you have to implement <UIPickerViewDelegate> Method as below
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([pickerView isEqual:FirstPickerView]) {
//SecondPickerView fill code
[SecondPickerView reloadAllComponents];
}
else {
//SecondPickerView Selected row
}
}
and change your second UIPickerView fill Loop accordingly
I would recommend using (NSInteger)component 0 is the first row and so on.
if (component == 0) {
if (row == 0) {
}
else if (row == 1)
{
}
}
else if (component == 1)
{
selectedPositionIndex = row;
if (row == 0) {
}
else if (row == 1)
{
}
else if (row == 2)
{
}
else if (row == 3)
{
}
}
This code works for just about all the picker view methods that provide the component NSInteger.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
You will need to make sure you return the right item count for each component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return wordArray.count;
}
else if (component == 1)
{
return positionArray.count;
}
return 0;
}

How to display UIlabel with string format when selecting data from a UIPickerview

Hi I have one picker view. This picker view from web services came
to loading data. This picker view is 2 part. Picker view 1. part is
Project name. and Picker view 2. part is Project number. When open
screen Project no label is no data. when open screen Project name
label no data. But when i rotate my picker view came to data this like
Proje Adı: "A1 Unitesi" Proje No: "002". How i can When open screen
came to data like rotate my picker ?
When open screen Project Name and Project No Labels no data. I want
to came data when open screen
When I rotate picker view came to data like this.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return [RaporlarList count];
}
return [RaporlarList count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
eObje = [RaporlarList objectAtIndex:row];
return eObje.ProjeAdii;
}
eObje = [RaporlarList objectAtIndex:row];
return eObje.ProjeNoo;
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0)
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje Adı : %#", [[RaporlarList objectAtIndex:row] ProjeAdii]];
lblProjeAdi.text = [NSString stringWithFormat:#"%#", resultString];
}
else
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje No: %#", [[RaporlarList objectAtIndex:row] ProjeNoo]];
lblProjeNo.text = resultString;
}
}
You have selected wrong datasource for pickerview.
You are returning string, instead return the label
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component
{
// return your UILabel
}
The correct answer to my question is as follows:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0)
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje Adı : %#", [[RaporlarList objectAtIndex:row] ProjeAdii]];
lblProjeAdi.text = [NSString stringWithFormat:#"%#", resultString];
}
else
{
NSString *resultString = [[NSString alloc] initWithFormat:#"Proje No: %#", [[RaporlarList objectAtIndex:row] ProjeNoo]];
lblProjeNo.text = resultString;
}
}

Resources