使用nskeyedarchiver切换视图时在屏幕上保留数据

时间:2013-07-25 03:16:42

标签: objective-c nsuserdefaults nskeyedarchiver nskeyedunarchiver

我已经使用xcode约9个月了,我即将完成我的应用程序,但有一个小问题......

一点背景知识。我有一个随机吐出一些数据的按钮[在这种情况下是锻炼]。在该窗口中,还有一个用于刷新数据和清除标签的按钮。

即使用户更改视图或退出应用,我也希望在屏幕上保留randomStretchLabel1,randomStretchLabel2。我希望它留在屏幕上,只有当用户点击randomButton或clearAllWorkouts时才能重新加载。

PIcture of table view to stretchViewController

@interface StretchViewController ()

{
    NSArray *stretchOptions;
}

- (IBAction)clearAllWorkouts:(id)sender;

- (IBAction)randomStretchButton1:(id)sender;
@property (retain, nonatomic) IBOutlet UILabel *randomStretchLabel1;
@property (retain, nonatomic) IBOutlet UILabel *randomStretchLabel2;
@end

@implementation StretchViewController

@synthesize randomStretchLabel1, randomStretchLabel2;


    - (IBAction)clearAllWorkouts:(id)sender {
    randomStretchLabel1.text = @"";
    randomStretchLabel2.text = @"";
}


- (IBAction)randomStretchButton1:(id)sender {
     stretchOptions = [[NSArray alloc]initWithObjects:@"90/90 Hamstring", @"Adductor", @"Adductor/Groin", @"All Fours Quad Stretch",@"Ankle Circles", @"Ankle On The Knee" @"Anterior Tibialis-SMR", @"Arm Circles", @"Behind Head Chest Stretch", @"Brachialis-SMR", @"Calf Stretch Elbows Against Wall", @"Calf Stretch Hands Against Wall", nil];


    int labelIndex = rand()%stretchOptions.count;
    [randomStretchLabel1 setText:[stretchOptions objectAtIndex:labelIndex]];


    int labelIndex2 = rand()%stretchOptions.count;
    [randomStretchLabel2 setText:[stretchOptions objectAtIndex:labelIndex2]];

    }

根据建议更改为以下但仍未获得我想要的内容。当我回到桌面视图(存储我所有其他锻炼的地方)时,标签会重置自己。

    - (void)archive
{
    [[NSUserDefaults standardUserDefaults] setValue:randomStretchLabel1.text forKey:@"randomStretchLabel1"];
    [[NSUserDefaults standardUserDefaults] setValue:randomStretchLabel2.text forKey:@"randomStretchLabel2"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)unarchive
{
    randomStretchLabel1.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"randomStretchLabel1"];
    randomStretchLabel2.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"randomStretchLabel2"];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
    - (void)viewDidUnload
    {
        [self setRandomStretchLabel1:nil];
        [self setRandomStretchLabel2:nil];
        [super viewDidUnload];

    }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc {
    [super dealloc];
}
@end

1 个答案:

答案 0 :(得分:0)

我希望我能正确理解你的问题。听起来您希望跨会话和视图转换持久保留标签的字符串值。最简单的方法是使用NSUserDefaults(下面的代码是一个例子,你可能想要做一些错误检查等):

- (void)archive
{
    [[NSUserDefaults standardUserDefaults] setValue:label1.text forKey:@"label_1"];
    [[NSUserDefaults standardUserDefaults] setValue:label2.text forKey:@"label_2"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)unarchive
{
    label1.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"label_1"];
    label2.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"label_2"];
}

或者您可以使用NSKeyedArchiver / NSKeyedUnarchiver方式:

- (void)archive
{        
    NSString * path1 = @"label1.archive";
    if ([NSKeyedArchiver archiveRootObject:label1.text toFile:path1]) {
        NSLog(@"Archive succeeded");
    } else {
        NSLog(@"Archive failed");            
    }
}

- (void)unarchive
{
    NSString * path1 = @"label1.archive";
    label1.text = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

这两种方法都处理所有基本的cocoa数据类型,因此您可以将它们用于NSArray的延伸等。

希望这有帮助,