Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将代码从本地文件保存更改为NSUserDefaults_Iphone_Objective C_Xcode - Fatal编程技术网

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

Iphone 将代码从本地文件保存更改为NSUserDefaults,iphone,objective-c,xcode,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 = [NSSearchP

我有一段代码,它调用并保存一个函数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会是什么代码吗?

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;

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;

我测试了此代码,但在第二次启动应用程序时未保存选定的ViewLeft或ViewRight。我必须改变什么?(别担心,这不是我的家庭作业)修改后的解决方案。它不起作用,因为您总是引用数组中的第一个对象(
[local objectAtIndex:0]
),但在保存时,您通过
addObject
将其添加到数组的末尾。因此,我已经将阵列全部清除,我认为您不需要它。如果您这样做,则像以前一样使用NSUserDefaults数组,但应用内委托使用
[[local lastObject]IsequalString:@”“]
工作!谢谢你的帮助!TomI测试了此代码,但在第二次启动应用程序时未保存选定的ViewLeft或ViewRight。我必须改变什么?(别担心,这不是我的家庭作业)修改后的解决方案。它不起作用,因为您总是引用数组中的第一个对象(
[local objectAtIndex:0]
),但在保存时,您通过
addObject
将其添加到数组的末尾。因此,我已经将阵列全部清除,我认为您不需要它。如果您这样做,则像以前一样使用NSUserDefaults数组,但应用内委托使用
[[local lastObject]IsequalString:@”“]
工作!谢谢你的帮助!汤姆