我已經搜遍了所有的堆棧溢出,但沒有答案真的幫我解決了我的問題,雖然它看起來很容易。從核心數據分配提取的數據到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] 由於您終止應用程序ncaught異常 'NSInternalInconsistencyException',原因: '請求在索引無效路徑的rect(2個索引[0,0])' * *第一擲調用堆棧: (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:終止叫做拋出異常
出於測試目的,當我運行viewDidLoad中的部分評論方法(評論所有關於cora數據),它工作正常。 如果我把viewDidAppear中的代碼放在viewDidLoad中,一旦viewController出現在屏幕上就立即崩潰
我摔倒了,就像我離真相不遠,但我找不到出路。 請問有人可以幫我嗎?
首先檢查你正在分配的核心數據的值是什麼,以及保持對UIPicker的委託方法的警惕查看 – iEinstein
我會試着回來...... – lweingart
好吧,那麼如果我直接去viewController我獲得的UIPickerView:2013-07-23 16:59:50.175列表元素MemeGen [45906:c07]:(實體:LolText; id:0xc03bac0 ;數據:),如果先通過UITableView進入PickerView,則獲得:2013-07-23 17:03:03.662 MemeGen [46266:c07]列表元素:< NSManagedObject:0x1046f680>(entity:LolText; id:0x1046e9c0 ; data:{ text =「TEST LOLTEXT」; }),但它仍然崩潰 –
lweingart