如何保存/检索 UIButton 状态?
这是我的第一个问题!
我将 UIButton 与 UISwitch 功能一起使用,并且我想保存/检索 UIButton 的状态。 请帮助!
解决了!
感谢 Kalle 和 aBitObvious!
我使用的代码
.h.m
@interface RetinaViewController : UIViewController {
IBOutlet UIButton *mybutton;
}
-(IBAction) toggleUIButtonImage:(id)sender;
@end
:
@implementation RetinaViewController
-(IBAction) toggleUIButtonImage:(id)sender{
NSString *value = @"ON";
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
UIImage *unselectedImage = [UIImage imageNamed: @"OFFa.png"];
UIImage *selectedImage = [UIImage imageNamed:@"ONa.png"];
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
value = @"OFF";
[userPreferences setObject:value forKey:@"stateOfButton"];
}else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
value = @"ON";
[userPreferences setObject:value forKey:@"stateOfButton"];
}
[userPreferences synchronize];
}
- (void)viewDidLoad {
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfButton"];
// If value is nil - disable the switch
if (value == nil) {
mybutton.selected = NO;
}
// If value is equal to ON
else if ([value compare:@"ON"] == NSOrderedSame) {
//NSLog(@"the switch is on");
// Set the switch to ON
mybutton.selected = YES;
} else {
//NSLog(@"the switch is off");
// Set the switch to OFF
mybutton.selected = NO;
}
[super viewDidLoad];
}
and this is my first question here !
I use UIButton with UISwitch functionality and i want to save/retrieve state of UIButton.
Pls HELP!
Resolved!
Thanks Kalle and aBitObvious !
code i use :
.h
@interface RetinaViewController : UIViewController {
IBOutlet UIButton *mybutton;
}
-(IBAction) toggleUIButtonImage:(id)sender;
@end
.m
@implementation RetinaViewController
-(IBAction) toggleUIButtonImage:(id)sender{
NSString *value = @"ON";
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
UIImage *unselectedImage = [UIImage imageNamed: @"OFFa.png"];
UIImage *selectedImage = [UIImage imageNamed:@"ONa.png"];
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
value = @"OFF";
[userPreferences setObject:value forKey:@"stateOfButton"];
}else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
value = @"ON";
[userPreferences setObject:value forKey:@"stateOfButton"];
}
[userPreferences synchronize];
}
- (void)viewDidLoad {
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfButton"];
// If value is nil - disable the switch
if (value == nil) {
mybutton.selected = NO;
}
// If value is equal to ON
else if ([value compare:@"ON"] == NSOrderedSame) {
//NSLog(@"the switch is on");
// Set the switch to ON
mybutton.selected = YES;
} else {
//NSLog(@"the switch is off");
// Set the switch to OFF
mybutton.selected = NO;
}
[super viewDidLoad];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在视图控制器的 viewDidLoad 中,输入如下内容:
In the viewDidLoad of the view controller, put something like this: