関数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 を使用するだけで、どのコードになるか誰か教えてもらえますか?