2016-06-13 75 views
0
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

我認爲這種線'countryLabel.text = countryArray [行]上;'應'countryLabel.text = arrayCountry [行];' –

+0

@ EICaptainv2 .0感謝它的作品,但我有另一個問題,當我選擇狀態按鈕它沒有顯示狀態列表 – Abhi

回答

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

謝謝它的作品,但我有另一個問題,當我選擇狀態按鈕它沒有顯示狀態列表 – Abhi

+0

你有statePicker。隱藏= YES;在你的代碼,所以,在按鈕操作你應該隱藏到false,並重新加載選擇器視圖將幫助 –

+0

請修復numberOfRowsInComponent,你應該使用其他if,if,if和else, –