我已经搜索了所有堆栈溢出,但没有任何答案能真正帮助我解决我的问题,尽管这似乎很容易。
我有一个应用程序,其中包含一个存储字符串的实体。我可以很好地存储它们并在 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] * Assertion failure in -[UITableViewRowData rectForRow: inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630 2013-07-23 10:51:55.012 MemeGen[51428:c07]Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path ( 2 indexes [0, 0])' * * First throw call stack: (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 called throwing an exception
出于测试目的,当我运行 viewDidLoad 方法的注释部分(注释所有关于 cora 数据的内容)时,它工作正常。如果我将 viewDidAppear 中的代码放在 viewDidLoad 中,只要 viewController 出现在屏幕上就会崩溃
我觉得我离真相不远,但我找不到出路。请问有人可以帮我吗?