Ich habe Viewcontroller mit einer globalen Eigenschaft ‚notesArray“, und ich möchte es von AppDelegate durch Singleton bekommen.Warum gibt sharedInstance null zurück?
Hier ist meine appViewController.h
#import <UIKit/UIKit.h>
@interface AppViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *notesArray;
}
@property (nonatomic, strong) NSMutableArray *notesArray;
+ (AppViewController*)sharedManager;
@end
Hier ist meine AppViewController.m
@implementation AppViewController
@synthesize notesArray;
+ (AppViewController*)sharedManager {
static AppViewController *sharedManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc] init];
});
return sharedManager;
}
...
@end
Meine app funktioniert gut, aber ich muss in NSUserDefaults es ist Staat zu retten, so in AppDelegate/applicationDidEnterBackground: ich
machen- (void)applicationDidEnterBackground:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setObject:[AppViewController sharedManager].notesArray forKey:@"savedNotes"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Aber das Problem ist, dass [AppViewController sharedManager].notesArray = nil
. Ich habe versucht, Breakpoint zu ApplicationDidEnterBackground: und alle Daten von [AppViewController sharedManager] = nil
.
Wo ist Ihr Code, der versucht, 'notesArray' zu initialisieren? Es wird "Null" sein, bis Sie ihm einen Wert zuweisen. – rmaddy