2013-07-23 70 views
0

我已經搜遍了所有的堆棧溢出,但沒有答案真的幫我解決了我的問題,雖然它看起來很容易。從核心數據分配提取的數據到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出現在屏幕上就立即崩潰

我摔倒了,就像我離真相不遠,但我找不到出路。 請問有人可以幫我嗎?

+0

首先檢查你正在分配的核心數據的值是什麼,以及保持對UIPicker的委託方法的警惕查看 – iEinstein

+0

我會試着回來...... – lweingart

+0

好吧,那麼如果我直接去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

回答

1

那麼,最後我改變了我的大部分代碼,它的工作到一定程度。現在最大的問題是,當我想將UIPickerView返回的值作爲字符串使用時,它是不可能的,我堅持使用類型值: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?

1

這是因爲你的Pickerview沒有行,這就是爲什麼系統崩潰。

您可以檢查您的列表是否提供數據?如果是的話,應該沒有問題。

檢查是否沒有數據,然後不打開pickerview。

+0

你好,我不確定我是否理解你的問題。如果我手動設置列表的內容,那麼它工作得很好。此外,我有一個UITableViewController,可以很好地顯示核心數據的數據。它應該表示它不是空的,不是嗎?或者我錯過了你的觀點? – lweingart

+0

認爲你可能意味着Ashutosh Mishra,我記錄了我的清單內容,並且它提供了數據。但是我確實在屏幕上看到了一個空的pickerview,並在我的開篇文章中寫到了日誌。 – lweingart

0

哈!終於做到了!問題是,在方法:

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

我被分配我的NSString這樣的:

_text = [self.list objectAtIndex:row]; 

,但分配的正確方法是:

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

問題解決了! 感謝您的幫助。