iPhone SDK:NSInvalidArgumentException -UIView numberOfComponentsInPickerView

时间:2010-02-05 16:38:04

标签: iphone

我正在尝试在UIPicker上开发一个简单的搜索机制。我使用的方法是保留两个数组。我的问题是由于某些原因我得到了这个运行时错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView numberOfComponentsInPickerView:]

以下是数组声明。

//data source for UIPicker NSArray
*arrayCountryChoices;

//search results buffer 
NSMutableArray *arraySearchResults; 

//properties
@property(nonatomic,retain) NSArray*arrayCountryChoices; 
@property(nonatomic,retain) NSMutableArray *arraySearchResults;

这是我初始化数据的地方

//create data
  arrayCountryChoices = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];

//copy the original array to searchable array
arraySearchResults = [arrayCountryChoices mutableCopy];

选择器方法。

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arraySearchResults count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arraySearchResults objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//grab the selected country
strUserChoice = [arraySearchResults objectAtIndex:row];
}

以下是完整性的搜索代码,虽然在我们到达此处之前应用程序已经死亡,但尚未真正相关。

//filter on search term
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", strSearchText];
[arraySearchResults filterUsingPredicate: predicate];
[pickerCountry reloadComponent:0];

我还拖动了数据源并将UIPicker中的连接委托给Interface Builder中的Files Owner。 有任何想法吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

看起来你已经将选择器的数据源设置为实现你在那里发布的代码的对象以外的其他东西 - 显然是某个地方的UIView。确保选择器的出口指向您的实际数据源对象。