将核心数据中提取的数据分配给UIPickerView

时间:2013-07-23 09:19:14

标签: ios objective-c core-data uipickerview

我已经搜索了所有堆栈溢出但没有回答真的帮助我解决了我的问题,虽然它似乎很容易。

我有一个带有实体的应用程序,我在其中存储字符串。我可以存储它们并在UITableView中很好地获取它们,但在某些时候我需要获取它们并将它们放在UIPickerView中。 这是我的班级,其中是UIPickerView。 由于它是在模态视图中,有一个委托在父视图中恢复数据,但这很好。 头文件:

#import <UIKit/UIKit.h>


@protocol TextChoiceDelegate <NSObject>
- (void) takeBackViewController:(id)controller didFinishSelectingText:(NSString *)text;
@end

@interface ChooseTextViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
    UIPickerView *pickerView;
    NSMutableArray *list;
}

@property (nonatomic, assign) id <TextChoiceDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (strong, nonatomic) NSString *text;

- (IBAction)didFinishChoosingText:(id)sender;

@end

.m文件:

#import "ChooseTextViewController.h"

@interface ChooseTextViewController ()
@property (strong) NSMutableArray *list;
@end


@implementation ChooseTextViewController

@synthesize delegate;

@synthesize pickerView;
@synthesize list;
@synthesize text;


#pragma mark - IBActions

- (IBAction)didFinishChoosingText:(id)sender
{
    NSString *textToPassBack = text;
    NSLog(@"returning: %@", textToPassBack);
    [self.delegate takeBackViewController:self didFinishSelectingText:textToPassBack];

    [self dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - UIPickerViewDataSource methods

// Number of components in the pickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// Number of elements in the pickerView component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
    return [self.list count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.list objectAtIndex:row];
}


#pragma mark - PickeView delegate methods

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"Selection of element: %@", [self.list objectAtIndex:row]);
    text = [self.list objectAtIndex:row];
}


#pragma mark - View Controller LifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    /*
    list = [[NSMutableArray alloc] init];
    [list addObject:@"1"];
    [list addObject:@"2"];
    [list addObject:@"3"];
    [list addObject:@"4"];
    [list addObject:@"5"];
     */
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.list = [[NSMutableArray alloc] init];

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"LolText"];

    self.list = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}


#pragma mark - Core Data Stack Methods

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id localDelegate = [[UIApplication sharedApplication] delegate];
    if ([localDelegate performSelector:@selector(managedObjectContext)]) {
        context = [localDelegate managedObjectContext];
    }
    return context;
}

@end

当我运行它时,pickerView显示为空,一旦我点击它,应用程序就会崩溃并显示以下错误日志: 2013-07-23 10:51:55.010 MemeGen [51428:c07] *断言失败 - [UITableViewRowData rectForRow:inSection:],/ SourceCache / UIKit_Sim / UIKit-2380.17 / UITableViewRowData.m:1630 2013-07-23 10:51:55.012 MemeGen [51428:c07] 由于未捕获的异常终止应用程序&#39; NSInternalInconsistencyException&#39;,原因:&#39 ;请求无效索引路径的rect(2个索引[0,0])&#39; * *第一次抛出调用堆栈: (0x22bb012 0x16e5e7e 0x22bae78 0xe6b665 0x502f20 0x3c62de 0x756086 0x755f7a 0x37940d 0x37b9eb 0x5e485a 0x5e399b 0x5e50df 0x5e7d2d 0x5e7cac 0x5dfa28 0x34c972 0x34ce53 0x32ad4a 0x31c698 0x264fdf9 0x264fad0 0x2230bf5 0x2230962 0x2261bb6 0x2260f44 0x2260e1b 0x264e7e3 0x264e668 0x319ffc 0x27ad 0x26d5) libc ++ abi.dylib:terminate调用抛出异常

出于测试目的,当我运行viewDidLoad方法的注释部分(评论所有关于cora数据)时,它工作正常。 如果我将viewDidAppear中的代码放在viewDidLoad中,只要viewController出现在屏幕上就会崩溃

我堕落了,因为我离真相不远,但我无法找到出路。 请问有人帮助我吗?

3 个答案:

答案 0 :(得分:1)

这是因为您的Pickerview没有行,这就是系统崩溃的原因。

您可以检查您的列表是否提供数据?如果是那么应该没有问题。

检查是否没有数据,然后不要打开pickerview。

答案 1 :(得分:1)

好吧,最后我改变了我的大部分代码,它确实适用于某一点。现在最大的问题是,当我想将UIPickerView返回的值用作String时,这是不可能的,我坚持使用类型值:NSKnownKeysDictionary1。

这里有问题的部分,但我找不到获取简单字符串的方法。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Core Data
    if (_managedObjectContext == nil)
    {
        _managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"LolText"];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LolText" inManagedObjectContext:_managedObjectContext];
    request.resultType = NSDictionaryResultType;
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"text"]];
    request.returnsDistinctResults = YES;


    _list = [[_managedObjectContext executeFetchRequest:request error:nil] mutableCopy];

    for (id key in _list) {
        NSLog(@"class of %@ = %@", key, [key class]);
        NSLog(@"string: %@", (NSString *)[[_list objectAtIndex:1] allValues]);
    }

    NSLog (@"texts: %@",_list);
}

以下是日志:

2013-07-23 22:23:16.178 MemeGen[79886:c07] class of {
    text = "MY TEST LOLTEXT";
} = NSKnownKeysDictionary1
2013-07-23 22:23:16.178 MemeGen[79886:c07] string: (
    "MY TEST LOLTEXT!"
)

我把它用于NSString的事实并没有解决问题。每次我使用NSString方法时,我总是以相同的“无法识别的选择器错误”结束,例如:

2013-07-23 22:38:06.497 MemeGen [80800:c07] - [NSKnownKeysDictionary1 stringByReplacingCharactersInRange:withString:]:无法识别的选择器发送到实例0x94e0a60 2013-07-23 22:38:06.498 MemeGen [80800:c07] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [NSKnownKeysDictionary1 stringByReplacingCharactersInRange:withString:]:发送到无法识别的选择器实例0x94e0a60'

从核心数据中获取数据时,有没有办法获取NSKnownKeysDictionary1?

答案 2 :(得分:0)

哈!终于做到了!问题在于方法:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

我正在分配我的NSString:

_text = [self.list objectAtIndex:row];

但是分配它的正确方法是:

_text = _list[row][@"text"];

问题解决了! 谢谢你的帮助。