2013-01-22 35 views
1

我不断收到这个错误,当我尝试将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作为段控制的数据源。

+0

'NSarray arrayuserlocation'应该是'NSarray * arrayuserlocation',对吧?什么是'loadAllUserLocation'内?看起来它不是NSString。这就是它崩溃的原因。 – iDev

+0

userlocation中有一个int属性。这是问题吗? –

+0

根据[文档](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

回答

1

正如我在评论中提到的那样,问题是您要通过userlocation对象而不是NSStringUIImage所需的对象。

根据documentation您的items数组应该是“一个NSString对象(用于段标题)或UIImage对象(用于段图像)的数组。

你需要从用户位置为取琴弦,

NSarray *arrayuserlocation = [[[MMIStore defaultStore] loadAllUserLocation] valueForKey:@"locationNm"];//use the param name here 

这应该给你的对象数组中的所有字符串数组。

+0

你好,非常感谢你的洞察力。我应该删除这个问题吗? –

+0

@KevinKohler,谢谢你的接受。您不必在编辑中完成所有提及的操作。只需使用上面的行。它应该工作。 – iDev

+0

您不必删除它。编辑后这不是一个坏问题,应该帮助其他面临类似问题的人。 – iDev

0

问题是,arrayuserlocation数组应包含NSString s而不是NSManagedObjects

0

抛出异常的代码期望您通过它NSString(这是响应isEqualToString:的对象)。但是,您正在向它传递一个UserLocation对象。您需要使用UserLocation对象的字符串加载arrayuserlocation,而不仅仅是发送一个对象数组。

+0

啊,我会怎么做?它有一个int和字符串属性? :) –