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