0
當我開始我的應用程序,它進入homeview,並沒有顯示出保存在任何NSUserDefaults的現有調色板。但是當我點擊'新調色板'按鈕並返回時,它會顯示新的和現有的全部。無法達到這個底部。任何幫助表示讚賞。NSUserDefaults數據一開始不會顯示?
應用delegate.h:
@interface palettesAppDelegate : NSObject <UIApplicationDelegate> {
NSMutableArray *colourPalettesContainer;
NSUserDefaults *prefs;
}
@property (assign, readwrite) NSUserDefaults *prefs;
@property (assign, readwrite) NSMutableArray *colourPalettesContainer;
@end
應用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
我缺少的東西,或者在NSUserDefaults的代碼從未提及? – 2011-03-19 00:15:49
'dataCenter.prefs = [NSUserDefaults的standardUserDefaults];'最可能的數據中心是在這一點上爲零。但是仍然有很多代碼錯過了最後的猜測。所以我會先檢查它是否爲零。 – 2011-03-19 00:25:58
fluchtpunkt可能是正確的,特別是如果這是從界面生成器視圖加載爲NIB。它在實際應用程序委託實例化之前加載視圖。一般的經驗法則是不使用應用程序委託作爲「應用程序的中央數據源」,而是使用單例。 – Kalle 2011-03-19 09:21:44