简体   繁体   中英

NSUserDefaults data doesn't show up at first?

When i start my app, it goes into homeview, and doesn't show any existing palettes saved in NSUserDefaults. But when i click the 'new palette' button and go back, it shows the new one and all of the existing ones. Can't get to the bottom of this. Any help is appreciated.

app delegate.h:

@interface palettesAppDelegate : NSObject <UIApplicationDelegate> {
 NSMutableArray *colourPalettesContainer;
    NSUserDefaults *prefs;
}
@property (assign, readwrite) NSUserDefaults *prefs;
@property (assign, readwrite) NSMutableArray *colourPalettesContainer;
@end

app delegate.m:

#import "palettesAppDelegate.h"

@implementation palettesAppDelegate

@synthesize colourPalettesContainer, prefs;

- (void)dealloc {
    [colourPalettesContainer release];
    [super dealloc];
}


@end

Homeview.h:

#import <UIKit/UIKit.h>
#import "HandlingPalettes.h"

@interface HomeView : UIViewController {

    HandlingPalettes *handlingPalettes;

}


@end

Homeview.m:

#import "HomeView.h"
#import <QuartzCore/QuartzCore.h>


@implementation HomeView


- (void)viewDidLoad {
    [super viewDidLoad];
palettesAppDelegate *dataCenter = (palettesAppDelegate *)[[UIApplication sharedApplication] delegate];

dataCenter.prefs = [NSUserDefaults standardUserDefaults];
dataCenter.colourPalettesContainer = [dataCenter.prefs objectForKey:@"palettes"];

handlingPalettes = [[HandlingPalettes alloc] init];

    [handlingPalettes newPalette];

}

-(void)viewWillAppear:(BOOL)animated {


    NSLog(@"view will appear: %i", [dataCenter.colourPalettesContainer count]);
    int numberOfExisting = [dataCenter.colourPalettesContainer count];

}

- (void)dealloc {
    [handlingPalettes release];
    [super dealloc];
}


@end

HandlingPalettes.h:

#import <UIKit/UIKit.h>

@interface HandlingPalettes : UIViewController {

}


-(void)newPalette;



@end

HandlingPalettes.m:

#import "HandlingPalettes.h"
#import "HomeView.h"
#import "palettesAppDelegate.h"



@implementation HandlingPalettes


-(void)newPalette {

    palettesAppDelegate *dataCenter = (palettesAppDelegate *)[[UIApplication sharedApplication] delegate];

    //If this is the first palette
    if (dataCenter.colourPalettesContainer == nil) {
    dataCenter.colourPalettesContainer = [[NSMutableArray alloc] init];
    }
    //Add a new palette

        [dataCenter.colourPalettesContainer addObject:@"Test1", @"Test2", nil];


    NSLog(@"Handling:    %i", [dataCenter.colourPalettesContainer count]);

    [dataCenter.prefs setObject:dataCenter.colourPalettesContainer forKey:@"palettes"];
[dataCenter.prefs synchronize];
}
- (void)dealloc {
   [super dealloc];
}


@end

The problem is that, when you load the app, and your app delegate is called, you do not restore its contents from the NSUserDefaults. You are just creating it if its nil, but that does not solve the problem. You should populate the colourPalettesContainer array with values in your application:didFinishLaunchingWithOptions: method.

Furthermore, Kalle's comment above is relevant; you should not use your app delegate like that. The best approach IMHO is to wrap it in a custom class, that takes care of storing default values at first launch, manages versioning, provides a strongly-typed interface, and can be subclasses if required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM