Abhi Abhi - 5 months ago 10
iOS Question

Not showing the selected object from picker view on the label

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;


}

Answer

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 :)

Comments