2012-11-19 34 views
1

我有調用並保存ViewLeft或ViewRight函數的代碼。當您重新啓動應用程序選定的視圖被加載。將本地文件保存更改爲NSUserDefaults

#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

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]; 
} 

而在你的應用程序代理:

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; 
+0

我測試此代碼,但不保存選擇ViewLeft或ViewRight。三名亞軍當我開始申請第二次。我必須改變什麼? (請不要擔心它不是我的作業) – Tom

+0

我從AppDelegate.m中添加了代碼 – Tom

+0

修改後的解決方案。它不工作,因爲你總是引用數組中的第一個對象('[local objectAtIndex:0]'),但是當你保存時,你通過'addObject'將它添加到數組的末尾。因此,我一起擺脫了陣列,我不認爲你需要它。如果你這樣做,然後像使用NSUserDefaults一樣使用數組,但在應用程序委託中使用'[[local lastObject] isEqualToString:@「」]' – WDUK