我不斷收到這個錯誤,當我嘗試將NSObject的綁定到分段控制UISegmentedControl CoreData綁定錯誤
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作爲段控制的數據源。
'NSarray arrayuserlocation'應該是'NSarray * arrayuserlocation',對吧?什麼是'loadAllUserLocation'內?看起來它不是NSString。這就是它崩潰的原因。 – iDev
userlocation中有一個int屬性。這是問題嗎? –
根據[文檔](http://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instm/UISegmentedControl/initWithItems :)您的items數組應該是_「一個NSString對象(用於片段標題)或UIImage對象(用於片段圖像)的數組。」_那麼你可以添加完整的'loadAllUserLocation'方法來顯示它的返回類型嗎? – iDev