UISegmentedControl CoreData绑定错误

时间:2013-01-22 19:12:31

标签: ios core-data

当我尝试将nsobject绑定到段控件时,我一直收到此错误

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作为段控件的数据源。

3 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,问题是您要传递userlocation个对象而不是NSStringUIImage个对象。

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

您需要从用户位置获取字符串,

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

这应该为您提供一个包含对象数组中所有字符串的数组。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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