#import "ViewController.h"
#define kStateComponent 0
#define kZipcomponent 1
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *pick;
@property (strong, nonatomic) NSDictionary *dataDic;
@property (nonatomic, strong) NSArray *firstArr;
@property (nonatomic,strong) NSArray *secondArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
_pick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 210, 320, 80)];
_pick.dataSource = self;
_pick.delegate = self;
_pick.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_pick];
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:@"statedictionary" withExtension:@"plist"];
self.dataDic = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSArray *allData = [self.dataDic allKeys];
NSArray *sortedStates = [allData sortedArrayUsingSelector:@selector(compare:)];
self.firstArr = sortedStates;
NSString *selectedState = self.firstArr[0];
self.secondArr = self.dataDic[selectedState];
}
#pragma mark -Picker Datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == kStateComponent) {
return [self.firstArr count];
} else {
return [self.secondArr count];
}
}
#pragma mark - Picker Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == kStateComponent) {
return self.firstArr[row];
} else {
return self.secondArr[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == kStateComponent) {
NSString * selectedState = self.firstArr[row];
self.secondArr = self.dataDic[selectedState];
[_pick reloadComponent:kZipcomponent];
[_pick selectRow:0 inComponent:kZipcomponent animated:YES];
}
NSInteger stateRow = [self.pick selectedRowInComponent:kStateComponent];
NSInteger zipRow = [self.pick selectedRowInComponent:kZipcomponent];
NSString *state = self.firstArr[stateRow];
NSString *zip = self.secondArr[zipRow];
NSString * title = [[NSString alloc] initWithFormat:@"You selected zip code %@",zip];
NSString * message = [[NSString alloc] initWithFormat:@"%@ is in %@",zip,state];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat pickerWidth = _pick.bounds.size.width;
if (component == kZipcomponent) {
return pickerWidth / 3;
} else {
return 2 * pickerWidth / 3;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end