当我尝试将 nsobject 绑定(bind)到段控件时,我不断收到此错误
UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60
2013-01-22 12:44:58.115 Momentum[39936:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60'
我已验证我的核心数据对象有数据。
NSarray *arrayuserlocation = [[MMIStore defaultStore] loadAllUserLocation];
UISegmentedControl *segControl = [[UISegmentedControl alloc]initWithItems:arrayuserlocation];
[segControl addTarget:self action:@selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setTintColor:[UIColor grayColor]];
编辑
回答下面的问题
- (NSMutableArray *)loadAllUserLocation
{
if (!allItems) {NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"UserLocation"];
[request setEntity:e]
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed"
format:@"Reason: %@", [error localizedDescription]];
}
allItems = [[NSMutableArray alloc] initWithArray:result];
}
return allItems;
它返回一个数组
我能够通过执行以下操作解决我的问题。
NSArray *arraylocation = [[MMIStore defaultStore] loadAllUserLocation];
NSMutableArray *newarray = [[NSMutableArray alloc] init];
for (UserLocation *user in arraylocation)
{
NSLog(@"%@ found", user.locationNm);
[newarray addObject:user.locationNm];
}
并使用 newarray 作为段控件的数据源。
最佳答案
正如我在评论中提到的,问题在于您传递的是 userlocation
对象,而不是所需的 NSString
或 UIImage
对象。
根据 documentation您的项目数组应该是“NSString 对象数组(用于片段标题)或 UIImage 对象(用于片段图像)”。
您需要从用户位置获取字符串,
NSarray *arrayuserlocation = [[[MMIStore defaultStore] loadAllUserLocation] valueForKey:@"locationNm"];//use the param name here
这应该为您提供对象数组中所有字符串的数组。
关于ios - UISegmentedControl CoreData 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14466211/