0

I'm still very new to Xcode and Obj-C programming so apologies in advance.

Using this site and some others, I have managed to piece together a pickerview with two components that upon selecting one of the options in the first component the second component is automatically altered to display options relevant to the option selected in the first component. The example picker view that I got working uses Countries and Football clubs in those countries e.g. If you select England in the first component you get a list of English teams in the second component, if you select Spain in the first component you get a list of Spanish teams in the second component etc.

Currently all these Countries and teams are all hardcoded within Xcode. What I wan't to do is have these counties and teams stored on a MySQL database and then send these records to the app using php. After that I want to use these json records in an array and display them on the picker view.

I have no problem getting the json data onto the app but when it comes to using the json with the picker view on the app then I have some problems. I have had no luck getting the picker view to work with the json array, it's starting to get pretty frustrating

Heres the code for the pickerview that I currently have with the hardcoded counties and teams:

#import "ViewController.h"

@interface ViewController ()
{
NSMutableArray *Nations;
NSMutableArray *England;
NSMutableArray *Espana;
NSMutableArray *Netherlands;
NSMutableArray *Germany;
NSMutableArray *Italy;

// Define keys
NSString *club;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Nations = [[NSMutableArray alloc]initWithObjects:@"England",@"Espana",@"Netherlands",@"Germany",@"Italy", nil];

England=[[NSMutableArray alloc]initWithObjects:@"Arsenal",@"Chelsea",@"Manchester City",@"Manchester United",@"Liverpool",@"Tottenham",@"Fulham City",@"Stoke City",@"Sunderland",@"NewCastle United",@"Blackburn Rovers",@"Southampton",@"Wolvers",@"Aston Villa", nil];

Espana = [[NSMutableArray alloc]initWithObjects:@"Barcelona",@"Real Madrid",@"Valencia",@"Athletico Madrid",@"Athletico Balbao",@"Getafe CF",@"Sevilla CF", nil];

Netherlands = [[NSMutableArray alloc]initWithObjects:@"Celtics",@"Ajax",@"Amesterdam", nil];

Germany = [[NSMutableArray alloc]initWithObjects:@"Bayern Munich",@"Bermen",@"Fiorentina",@"Pampas",@"Nord", nil];

Italy = [[NSMutableArray alloc]initWithObjects:@"AC Milan",@"Inter Milan",@"Juventus", nil];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component ==0)
    {
        return [Nations count];
    }
    else {
        if ([club isEqualToString:@"Espana"]) {
            return [Espana count];
        }
        if ([club isEqualToString:@"Germany"]) {
            return [Germany count];
        }
        if ([club isEqualToString:@"Netherlands"]) {
            return [Netherlands count];
        }
        if ([club isEqualToString:@"Italy"]) {
            return [Italy count];
        }
        else  {
            return [England count];
        }
    }

    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:    (NSInteger)component
{
    if(component ==0)
    {
        return [Nations objectAtIndex:row];
    }
    else {
        if ([club isEqualToString:@"Espana"]) {
            return [Espana objectAtIndex:row];
        }
        if ([club isEqualToString:@"Germany"]) {
            return [Germany objectAtIndex:row];
        }
        if ([club isEqualToString:@"Netherlands"]) {
           return [Netherlands objectAtIndex:row];
        }
        if ([club isEqualToString:@"Italy"]) {
            return [Italy objectAtIndex:row];
        }
        else  {
            return [England objectAtIndex:row];


        }
    }

    return 0;

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
        [pickerView reloadComponent:1];
    }
 }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 - (void)dealloc {
}

@end

2 Answers 2

0

What exactly is happening? Your code looks absolutely fine.

Did you set up your UIPickerView?

In viewDidLoad it should look like this:

UIPickerView * mainPickerView = [[UIPickerView alloc] init];
[mainPickerView setShowsSelectionIndicator:YES];
mainPickerView.backgroundColor = [UIColor whiteColor];
mainPickerView.dataSource = self;
mainPickerView.delegate = self;

Make sure the datasource and the delegate are set. Also make sure you have the right frame. And, overall, make sure to call [mainPickerView reloadAllComponents]; when you are done getting the info from the server if your UIPickerView is visible.

Feel free to ask questions.

So you basically use some networking framework to get your JSON server answer which 90% of the time will be an NSDictionary that looks like this:

NSDictionary *result=[NSJSONSerialization JSONObjectWithData:resultData options:kNilOptions error:&error];

Inside that dictionary you have some objects that are practically NSArrays. Just replace your existing NSArrays with them, reload your UIPicker and you're good to go.

Sign up to request clarification or add additional context in comments.

6 Comments

No theres nothing wrong with the code shown above, it works fine.
What I want is to alter this code so that instead of normal arrays for countries and teams it uses json data. My problem is I don't know how to go about it.
@user3406608 then it isn't the problem of UIPicker, it's the problem of you parsing the JSON. And it's a matter of a different answer. I'll update mine asap.
@user3406608 yes, in fact you can do with the data you had in JSON anything you want.
|
0

It's frustrating because it's hard to keep track of different arrays. I suggest you change your data structure to a NSDictionary of dictionaries. Your JSON should look the same way.

viewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


self.listOfclubs = 
@{
 @"Nations ":@[@"England",@"Espana",@"Netherlands",@"Germany",@"Italy"]
 @"England":@[@"Arsenal",@"Chelsea",@"Manchester City",@"Manchester United",@"Liverpool",@"Tottenham",@"Fulham City",@"Stoke City",@"Sunderland",@"NewCastle United",@"Blackburn Rovers",@"Southampton",@"Wolvers",@"Aston Villa"],
 @"Espana ":@[@"Barcelona",@"Real Madrid",@"Valencia",@"Athletico Madrid",@"Athletico Balbao",@"Getafe CF",@"Sevilla CF"],
 @"Netherlands":@[@"Celtics",@"Ajax",@"Amesterdam"],
 @"Germany  ":@[@"Bayern Munich",@"Bermen",@"Fiorentina",@"Pampas",@"Nord"],
 @"Italy  ":@[@"AC Milan",@"Inter Milan",@"Juventus"],
};
}

numberOfRowsInComponent

- (NSInteger)pickerView:(UIPickerView *)pickerView 
                                      numberOfRowsInComponent:(NSInteger)component
{
    return [listOfclubs objectForKey:club];
}

titleForRow

- (NSString *)pickerView:(UIPickerView *)pickerView 
                     titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [[listOfclubs objectForKey:club] objectAtIndex:row];
}

Look at how simple and flexible this code is compared to previous one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.