保存 UIButton 状态并使用 NSUserDefaults 加载

发布于 2024-12-03 00:29:00 字数 126 浏览 2 评论 0原文

我想知道如何检测按钮是否隐藏,以及对其应用了多少 Alpha。然后,当调用 viewDidLoad 时,可以应用这些值以使按钮与应用程序关闭时保持的状态相同。

我该如何编码?

谢谢,

詹姆斯

I was wondering how to detect whether the button was hidden or not, and how much alpha was applied to it. Then, when viewDidLoad is called, these values can be applied to make the buttons the same state they were left in when the application closed.

How can I code this?

Thanks,

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

流年里的时光 2024-12-10 00:29:00

如果,正如您的标题所示,您想使用 NSUserDefaults,那么您可以这样设置它们:

-(void)saveButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.hidden forKey:@"isHidden"];
    [defaults setFloat:button.alpha forKey:@"alpha"];
}

-(void)restoreButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    button.hidden = [defaults boolForKey:@"isHidden"];
    button.alpha = [defaults floatForKey:@"alpha"];
}

如果您想对多个按钮执行此操作,那么您可以使用 tag s 在默认值中区分它们:

-(void)saveButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.hidden forKey:[NSString stringWithFormat:@"isHidden%d",button.tag]];
    [defaults setFloat:button.alpha forKey:[NSString stringWithFormat:@"alpha%d",button.tag]];
}

-(void)restoreButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    button.hidden = [defaults boolForKey:[NSString stringWithFormat:@"isHidden%d",button.tag]];
    button.alpha = [defaults floatForKey:[NSString stringWithFormat:@"alpha%d",button.tag]];
}

If, as your title suggests, you want to use NSUserDefaults, then you can set them like this:

-(void)saveButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.hidden forKey:@"isHidden"];
    [defaults setFloat:button.alpha forKey:@"alpha"];
}

-(void)restoreButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    button.hidden = [defaults boolForKey:@"isHidden"];
    button.alpha = [defaults floatForKey:@"alpha"];
}

If you want to do this for multiple buttons, then you can use tags to differentiate between them in the defaults:

-(void)saveButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.hidden forKey:[NSString stringWithFormat:@"isHidden%d",button.tag]];
    [defaults setFloat:button.alpha forKey:[NSString stringWithFormat:@"alpha%d",button.tag]];
}

-(void)restoreButtonState:(UIButton*)button {
    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    button.hidden = [defaults boolForKey:[NSString stringWithFormat:@"isHidden%d",button.tag]];
    button.alpha = [defaults floatForKey:[NSString stringWithFormat:@"alpha%d",button.tag]];
}
我们的影子 2024-12-10 00:29:00

要检查按钮状态,请检查此帖子: Xcode:检查 UIButton是隐藏的,使用 NSUserDefaults

关于Alpha,尝试求一下返回值:self.button.alpha;

您可以更新 SQLite 数据库中按钮和 Alpha 按钮的状态(值)。在 viewDidLoad 方法中,从数据库回调值并立即应用它们。关于如何做到这一点,请在网上查看更多信息,您就会到达那里,因为互联网上充满了有关该主题的教程。嗯,这就是我要做的,SQLite 实际上非常有用;但也许还有其他方法。

To check out the button state, check this post: Xcode: Check if UIButton is hidden, using NSUserDefaults

Regarding the Alpha, try to ask for the return value of: self.button.alpha;

you can update the state (value) of the button and the Alpha button in an SQLite database. In the viewDidLoad method, call back the values from the database and apply them immediately. On how to do this, check out some more info on the web and you'll get there because the internet is filled with tutorials on the topic. Well, that's what I would do, SQLite is really useful actually; but perhaps there are other ways as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文