如何保存/检索 UIButton 状态?

发布于 2024-09-27 21:47:33 字数 1647 浏览 6 评论 0原文

这是我的第一个问题!

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2024-10-04 21:47:33

在视图控制器的 viewDidLoad 中,输入如下内容:

NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
NSString *stateOfButton = [userPreferences stringForKey:@"stateOfButton"];
if ([stateOfButton isEqualToString:@"ON"])
{
    //set button state to "selected"
}
else
{
    //set button state to "not selected"
}

In the viewDidLoad of the view controller, put something like this:

NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
NSString *stateOfButton = [userPreferences stringForKey:@"stateOfButton"];
if ([stateOfButton isEqualToString:@"ON"])
{
    //set button state to "selected"
}
else
{
    //set button state to "not selected"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文