我已经使用 xcode 大约 9 个月了,我即将完成我的应用程序,但有 1 个小问题......
一点背景。我有一个随机吐出一些数据的按钮[在这种情况下是锻炼]。在该窗口中还有一个按钮可以刷新数据并清除标签。
即使用户更改视图或退出应用程序,我也希望将 randomStretchLabel1、randomStretchLabel2 保留在屏幕上。我希望它留在屏幕上,并且只能在用户点击 randomButton 或 clearAllWorkouts 时重新加载。
@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