我有一个简单的 View Controller ,它在显示“继续游戏”按钮之前从数据库加载记录。我的记录已加载到 _company 变量中,我可以确认它已正确填充。
但是,当prepareForSegue运行时,该变量为空。
这很奇怪,因为我尝试在 _company 变量更新的同时创建一个字符串实例,并且这在准备...方法中可用。
// 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
非常感谢对此的任何帮助,因为它让我完全难住了!
最佳答案
为什么将其声明为弱属性?谁拥有该特性?我有一种感觉,将此声明更改为强应该可以解决问题。
@property (weak, nonatomic) IBOutlet UIButton *continueGameButton;
@property (strong, nonatomic) Company *company;
@property (strong, nonatomic) NSString *name;
关于iphone - 对于prepareForSegue方法,实例变量为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508677/