我有一个单例类,我在其中声明了一个属性:
@property (nonatomic, strong) NSString *currentTableName;
+ (SuperNoteManager*)sharedInstance;
.m文件:
+ (SuperNoteManager*)sharedInstance
{
static SuperNoteManager *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[SuperNoteManager alloc] init];
});
return _sharedInstance;
}
当我第一次运行我的应用时,数据库中没有数据,因此显示EmptyViewController
。
@property (nonatomic, strong) SuperNoteManager *myManager;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
_myManager=[SuperNoteManager sharedInstance];
}
-(void)changeRootView{
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
HomeViewController *hVC=[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
UINavigationController *mNavVC=[storyboard instantiateViewControllerWithIdentifier:@"MainNavigationController"];
mNavVC.viewControllers=@[hVC];
[[UIApplication sharedApplication].keyWindow setRootViewController:mNavVC];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if ( [_myManager checkForDataInAllTables]) {
NSLog(@"All tables are empty");
}else{
//a note is saved, show home view controller
if (![_myManager isDatabaseEmpty]) {
[self changeRootView];
}
}
}
NavigationBar
上EmptyNotesViewController
上有+按钮,点按'+',
NotesViewController
被推出EmptyNotesViewController
。
在NotesViewController
中,在我写了一些笔记之后,我将笔记保存在数据库中:
NotesViewController
:
@property (nonatomic,strong) SuperNoteManager *myManager;
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_myManager.currentTableName=@"WorkTable";
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (self.isMovingFromParentViewController) {
NSLog(@"going back");
[self insertTextintoDatabase]; //Text is inserted . I double checked
}
}
然后当我回到我的EmpytNotesViewController
时,我会检查数据,如果有数据,我会更改rootViewController
,因为它不再是EmptyNotesView
。
所以当我回到我的EmptyNotesViewController
:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if ( [_myManager checkForDataInAllTables]) {
NSLog(@"All tables are empty");
}else{
//a note is saved, show home view controller
//Put a breakpoint here
if (![_myManager isDatabaseEmpty]) {
[self changeRootView];
}
}
}
此处断点_myManager.currentTableName
为nil
。的为什么吗
我在NotesController
中设置它,当它回到EmptyNotesController
时它变为零。
我认为一旦在单身中设置了一个值,只要应用程序被关闭/杀死,它就会持续存在。
Note
:我已经宣布我的Singleton类的属性很强,并且单例中的所有属性都被声明为强。
答案 0 :(得分:2)
您似乎永远不会在SuperNoteManager
中获得对NotesViewController
单身人士的引用,就像您在EmptyNotesController
中所做的那样。
因此,currentTableName
属性永远不会首先设置。
您想要插入:
_myManager = [SuperNoteManager sharedInstance];
在设置-viewDidAppear:
属性之前,在currentTableName
中。