0

如何在删除其父视图后保存按钮的背景颜色状态。所以下次我在再次加载父视图后单击按钮时,我可以更改按钮的初始颜色。

4

1 回答 1

1

你可以去PList

试试这个:-
在 AppDelegate.h

+(NSString *)getValueFromPlist:(NSString *)key filename:(NSString *)filename;
+(NSString *)dataFilePath:(NSString *)filename;

在 AppDelegate.m

+(NSString *)getValueFromPlist:(NSString *)key filename:(NSString *)filename
{
    NSString *path = [AppDelegate dataFilePath:filename];
    //NSLog(@"\npath - %@\n", path);
    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
    //NSLog(@"\nparsing- %@", dict);
    return [dict valueForKey:key];
}

+ (NSString *)dataFilePath:(NSString *)filename {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [paths objectAtIndex:0];
    return [docDirectory stringByAppendingPathComponent:filename];
}

yourViewController.m中,使用此代码将值保存在 plist 中:-

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"Your Value to be saved" forKey:@"value"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"nameOfPlist.plist"];

[dict writeToFile:path atomically:YES];

下次,从 plist 中获取值并将其用于您的代码:-

NSString *strValue = [AppDelegate getValueFromPlist:@"value" filename:@"nameOfPlist.plist"];
于 2013-12-19T11:59:35.343 回答