0

I have been using xcode for ~9 months now and I am close to finishing my app but have 1 small problem...

A little bit of background. I have a button that randomly spits out some data [workouts in this case]. In that window there is also a button to refresh the data and clear out the labels.

I want to keep randomStretchLabel1, randomStretchLabel2 on the screen even when user changes views or gets out of app. I want it to stay on the screen and only be able to reload when user hits the randomButton or 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]];

    }

Changed to following based on suggestion but still not getting what I want. Labels reset themselves when I go back to the table view (where all my other workouts are stored).

    - (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
KatDJ
  • 1
  • 1
  • If your data is pretty simple, NSKeyedArchiver is the easiest way to persist your data and the object graph. Here's an easy-to-read overview: http://nshipster.com/nscoding/ – Aaron Brager Jul 25 '13 at 03:28
  • Why isn't core data working for you? – Ahmed Z. Jul 25 '13 at 03:28
  • I would also recommend CoreData. You can store your workouts etc as objects, and maintain a reference to the active objects. Your view / view controller could then display those active objects when loaded; refresh could change them. – sapi Jul 25 '13 at 03:41

1 Answers1

0

I hope I'm understanding your question correctly. It sounds like you want to persist the string values of the labels across sessions and view transitions. The simplest way to do this is to use NSUserDefaults (the code below is an example, you probably want to do some error checking etc):

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

Or you can use the NSKeyedArchiver / NSKeyedUnarchiver approach like so:

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

Both approaches handle all of the basic cocoa datatypes so you could use them for your NSArray of stretches etc.

Hope this helps,

Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
  • Still not working for me.... I tried NSUserDefault and NSKeyedArchiver. I updated my code so you can see where I'm at. – KatDJ Aug 09 '13 at 15:53