不显示从标签上的选取器视图中选择的对象
Posted
技术标签:
【中文标题】不显示从标签上的选取器视图中选择的对象【英文标题】:Not showing the selected object from picker view on the label 【发布时间】:2016-06-13 05:34:46 【问题描述】: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:
【问题讨论】:
我认为countryLabel.text = countryArray[row];
这一行应该是countryLabel.text = arrayCountry[row];
@EICaptainv2.0 感谢它的工作,但我有另一个问题,当我选择状态按钮它没有显示状态列表
【参考方案1】:
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 :)
【讨论】:
感谢它的工作,但我在选择状态按钮时遇到另一个问题,它没有显示状态列表 你有 statePicker.hidden = YES;在您的代码中,在按钮操作上您应该隐藏为 false 并重新加载选择器视图会有所帮助 请修正 numberOfRowsInComponent ,你应该使用 else if , insted of if, if 和 else, 点击状态按钮时崩溃,崩溃日志为:-[__NSCFDictionary length]: unrecognized selector sent to instance 0x7f98c9683220 2016-06-13 11:30:54.912 National_black[1745:52321] ** * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary 长度]:无法识别的选择器发送到实例 0x7f98c9683220”*** 第一次抛出调用堆栈: 添加异常断点并查看它崩溃的位置,根据您尝试使用字典中的方法“length”的崩溃日志,它应该与 NSString 一起使用,而不是与 NSDictionary 一起使用,以上是关于不显示从标签上的选取器视图中选择的对象的主要内容,如果未能解决你的问题,请参考以下文章