ios - UISegmentedControl CoreData 绑定(bind)错误

标签 ios core-data

当我尝试将 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 对象,而不是所需的 NSStringUIImage 对象。

根据 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/

相关文章:

ios - 对多关系作为 sectionNameKeyPath

ios - 将核心数据中的 UIView 子类或 UIBezierPath 存储为可转换

ios - UIStackView : Image take most of the stack while Text shrink in fill distribution

ios - 游戏中心登录问题

swift - 使用核心数据中的索引来获取特定的二进制数据

objective-c - 核心数据和实体数n "Person"

ios - 监控和维护应用程序在磁盘上的大小的解决方案

ios - 有什么方法可以指出或删除 Objective-C 的 .m 文件中无用的导入

ios - 带有 Watch OS 1 和 2 的 Watch Kit 应用

iOS:关闭 View Controller 并推送新 View 而不在导航堆栈中显示当前 View