0

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

但是,prepareForSegue 运行时该变量为空。

这很奇怪,因为我尝试在 _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

将不胜感激任何帮助,因为它让我完全难倒!

4

1 回答 1

1

为什么这被声明为弱属性?谁拥有这处房产?我有一种感觉,将这个声明更改为 strong 应该可以解决问题。

@property (weak, nonatomic) IBOutlet UIButton *continueGameButton;
@property (strong, nonatomic) Company *company;
@property (strong, nonatomic) NSString *name;
于 2013-05-12T15:17:54.173 回答