prepareForSegue方法的实例变量为null

时间:2013-05-12 15:00:55

标签: iphone ios objective-c

我有一个简单的视图控制器,可以在显示“继续游戏”按钮之前从数据库加载记录。我记录加载到_company变量,我可以确认这是正确填充的。

但是,当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 个答案:

答案 0 :(得分:1)

为什么这被宣布为弱房产?谁拥有这家酒店?我有一种感觉,将这个声明改为强者应该可以解决问题。

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