0

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

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

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

表视图到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
4

1 回答 1

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

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

希望这可以帮助,

于 2013-08-08T03:06:32.067 回答