将代码从本地文件保存更改为NSUserDefaults

时间:2012-11-19 21:43:41

标签: iphone objective-c xcode

我有这个代码调用并保存ViewLeft或ViewRight函数。当您重新启动应用程序时,会加载所选视图。

#import "ViewController.h"
#import "ViewLeft.h"
#import "ViewRight.h"

@implementation ViewController

-(IBAction)submitL:(id)sender {
    str = @"L";
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *localloc = [documentsDirectory stringByAppendingPathComponent:@"/localinfo.plist"];
    NSMutableArray *local = [[NSMutableArray alloc] initWithContentsOfFile:localloc];
    if(!local) {
        local = [[NSMutableArray alloc] init];
    } else {
        local = [[NSMutableArray alloc] initWithArray:local];
    };
    [local addObject:str];
    if(![local writeToFile:localloc atomically:NO]) {
        NSLog(@"f1");
    };
    ViewLeft *view = [[ViewLeft alloc] initWithNibName:@"ViewLeft" bundle:nil];
    [self presentViewController:view animated:NO completion:nil];
}

-(IBAction)submitR:(id)sender {
    str = @"R";
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *localloc = [documentsDirectory stringByAppendingPathComponent:@"/localinfo.plist"];
    NSMutableArray *local = [[NSMutableArray alloc] initWithContentsOfFile:localloc];
    if(!local) {
        local = [[NSMutableArray alloc] init];
    } else {
        local = [[NSMutableArray alloc] initWithArray:local];
    };
    [local addObject:str];
    if(![local writeToFile:localloc atomically:NO]) {
        NSLog(@"f2");
    };
    ViewRight *view = [[ViewRight alloc] initWithNibName:@"ViewRight" bundle:nil];
    [self presentViewController:view animated:NO completion:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (! [defaults objectForKey:@"firstRun"]) {
    [defaults setObject:[NSDate date] forKey:@"firstRun"];
}

这是AppDelegate.m中的代码,用于选择适当的视图控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *localloc = [documentsDirectory stringByAppendingPathComponent:@"/localinfo.plist"];
    NSMutableArray *local = [[NSMutableArray alloc] initWithContentsOfFile:localloc];
    if(!local) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
    } else {
        local = [[NSMutableArray alloc] initWithArray:local];
        if([[local objectAtIndex:0] isEqualToString:@"L"]){
            self.ViewLeft = [[ViewLeft alloc] initWithNibName:@"ViewLeft" bundle:nil];
            self.window.rootViewController = [[ViewLeft alloc] initWithNibName:@"ViewLeft" bundle:nil];
        }
        if([[local objectAtIndex:0] isEqualToString:@"R"]){
            self.ViewRight = [[ViewRight alloc] initWithNibName:@"ViewRight" bundle:nil];
            self.window.rootViewController = [[ViewRight alloc] initWithNibName:@"ViewRight" bundle:nil];
        }
    };
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

有人可以告诉我使用NSUserDefault函数会是什么代码吗?

1 个答案:

答案 0 :(得分:1)

NSUserDefaults是一个简单的键值存储。没有太高级,你可以这样使用它:

-(IBAction)submitR:(id)sender {
    [self saveSubmission:@"R"];
    ViewRight *view = [[ViewRight alloc] initWithNibName:@"ViewRight" bundle:nil];
    [self presentViewController:view animated:NO completion:nil];
}

-(IBAction)submitL:(id)sender {
    [self saveSubmission:@"L"];
    ViewLeft *view = [[ViewLeft alloc] initWithNibName:@"ViewLeft" bundle:nil];
    [self presentViewController:view animated:NO completion:nil];
}

-(void)saveSubmission:(NSString*)submission { 
    NSUserDefaults* standardDefaults = [NSUserDefaults standardUserDefaults];

    [standardDefaults setObject:submission forKey:@"leftRightSubmission"];

    // Not always needed, this flushes changes to disk asap. Can be costly
    [standardDefaults synchronize];
}

在你的app代理中:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

NSUserDefaults* standardDefaults = [NSUserDefaults standardUserDefaults];

NSString* lastSubmission = [standardDefaults objectForKey:@"leftRightSubmission"];
if(!lastSubmission) {
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
} 
else {
    if([lastSubmission] isEqualToString:@"L"]){
        self.ViewLeft = [[ViewLeft alloc] initWithNibName:@"ViewLeft" bundle:nil];
        self.window.rootViewController = [[ViewLeft alloc] initWithNibName:@"ViewLeft" bundle:nil];
    }
    if([lastSubmission isEqualToString:@"R"]){
        self.ViewRight = [[ViewRight alloc] initWithNibName:@"ViewRight" bundle:nil];
        self.window.rootViewController = [[ViewRight alloc] initWithNibName:@"ViewRight" bundle:nil];
    }
};

[self.window makeKeyAndVisible];

return YES;