2013-05-12 52 views
0

我有一個簡單的視圖控制器,在顯示「繼續遊戲」按鈕之前從數據庫加載記錄。我記錄被加載到_company變量,我可以確認這是正確填充。實例變量爲null for prepareForSegue方法

但是,prepareForSegue運行時變量爲null。

這很奇怪,因爲我試圖在_company變量被更新的同時創建一個字符串實例,這在prepare ...方法中是可用的。

// StartScreenViewController.h 

@interface StartScreenViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIButton *continueGameButton; 
@property (weak, nonatomic) Company *company; 
@property (weak, nonatomic) NSString *name; 

- (void)setupGameButtons; 
- (void) getSavedGame; 

@end 


// StartScreenViewController.m 

@implementation StartScreenViewController 

@synthesize continueGameButton = _continueGameButton; 
@synthesize company = _company; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupGameButtons]; 
     // Do any additional setup after loading the view. 
} 

- (void)viewDidUnload 
{ 
// self.company = nil; 
    [self setContinueGameButton:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (void)setupGameButtons { 
    [self getSavedGame]; 
    if (_company == nil) { 
     _continueGameButton.hidden = YES; 
    } 
} 

- (void)getSavedGame { 
    NSError *error; 
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[Company entityName]]; 
    [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"name", nil]]; 
    [fetchRequest setFetchLimit:30]; 
    [fetchRequest setFetchBatchSize:30]; 
    NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]]; 
    NSArray *results = [[[DomainDataModel sharedDataModel] mainContext] executeFetchRequest:fetchRequest error:&error];  
    _company = [results objectAtIndex:0]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSLog(@"company name:: %@", [_company name]); 
// [self getSavedGame]; 
    if ([@"continue_game" isEqualToString:[segue identifier]]) { 
     DashboardViewController *controller = (DashboardViewController *)segue.destinationViewController; 
     controller.company = _company; 

    } 
} 

@end 

希望對此有所幫助,因爲它讓我完全難住!

回答

1

爲什麼這被宣佈爲弱財產?誰擁有這個屬性?我有一種感覺,把這個宣言改爲強大的應該是訣竅。

@property (weak, nonatomic) IBOutlet UIButton *continueGameButton; 
@property (strong, nonatomic) Company *company; 
@property (strong, nonatomic) NSString *name; 
+0

謝謝 - 看起來像我需要學習一些更多關於這些不同類型的:) – Ger 2013-05-12 15:42:38

+0

我看到它的方式 - 如果你不知道其中的差別,你應該停止任何東西,學習目標C。尊重技術:) – Stavash 2013-05-13 06:48:29

+3

我更喜歡學習,因爲你走的方法 - 不想過度尊重的東西;) – Ger 2013-05-13 15:05:14