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