I am using picker view in my application, I am selecting the object but object is not showing on the label,
Please help me
pragma mark- PICKER_VIEW_Delegate_Methods ----------------->>>>
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(pickerView == countryPicker)
{
return [arrayCountry count];
}
if(pickerView == statePicker)
{
return [stateArray count];
}
else return 5;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(pickerView == countryPicker)
{
return [arrayCountry objectAtIndex:row];
}
if(pickerView == statePicker)
{
return [stateArray objectAtIndex:row];
}
return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView == countryPicker)
{
countryLabel.text = countryArray[row];
countryPicker.hidden = YES;
NSLog(@"statelabel %@",countryLabel.text);
}
if(pickerView == statePicker)
{
[stateLabel setText:[stateArray objectAtIndex:row]];
statePicker.hidden = YES;
}
}
- (IBAction)countryList:(id)sender
{
countryPicker.hidden = false;
[countryPicker reloadAllComponents];
}
- (IBAction)stateList:(id)sender
{
stateArray = [DictStates valueForKeyPath:countryStr];
statePicker.hidden = false;
stateTable.hidden = FALSE;
[statePicker reloadAllComponents];
}
- (void)getCountryStatesList
{
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/countrystate"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"List : %@", json);
dispatch_async(dispatch_get_main_queue(), ^{
[self parseCountryStates:json];
});
}];
[dataTask resume];
}
- (void)parseCountryStates:(NSDictionary *)json
{
NSArray *listing = [json objectForKey:@"listing"];
arrayCountry = [[NSSet setWithArray:[listing valueForKey:@"country"]] allObjects];
NSArray *states;
NSMutableDictionary *tempStates = [NSMutableDictionary new];
for (NSString *countryName in arrayCountry) {
NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", @"country", countryName];
NSLog(@"predicate %@",predicateString);
states = [NSMutableArray arrayWithArray:[listing filteredArrayUsingPredicate:predicateString]];
[tempStates setObject:states forKey:countryName];
}
DictStates = tempStates;
}
now country is selected but now when i click on state button it creashed
i am sharing my crash log:
> -[__NSCFDictionary length]: unrecognized selector sent to instance 0x7f98c9683220
> 2016-06-13 11:30:54.912 [1745:52321] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
> '-[__NSCFDictionary length]: unrecognized selector sent to instance
> 0x7f98c9683220'
> *** First throw call stack:
答案 0 :(得分:0)
Replace Your method with
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView == countryPicker)
{
countryLabel.text = arrayCountry[row];
countryPicker.hidden = YES;
NSLog(@"statelabel %@",countryLabel.text);
}
if(pickerView == statePicker)
{
[stateLabel setText:[stateArray objectAtIndex:row]];
statePicker.hidden = YES;
}
}
Will solve your problem :)