我在视图上有一个自定义 UIButton,当我按下它时,它会改变状态和 img。当我退出视图并返回它时,UIButton 的状态保持不变。但是当我退出我的应用程序并重新启动它时。我的自定义 UIButton 的状态恢复为默认值。当我退出应用程序时,有没有办法可以保存该按钮的状态?
下面是一个示例图片
在 http://i.minus.com/ibzHa1XCTDwSAJ.png 之前
![之后] http://min.us/mtkB3JQRI
这是我试图在 -(void)viewDidLoad 上执行此操作的代码
UOEventPost *post = (UOEventPost *)self.event;
if(post.postWanted == YES){
[wantThis setBackgroundImage:[UIImage imageNamed:@"wanted@2x.png"] forState:UIControlStateSelected];
[wantThis setSelected:TRUE];
wantThis.titleLabel.text = [NSString stringWithFormat:@"Wanted"];
}else {
[wantThis setSelected:FALSE];
}
编辑
savedState = [[NSUserDefaults standardUserDefaults]boolForKey:@"TheSavedState"];
UOEventPost *post = (UOEventPost *)self.event;
if(post.postWanted == YES){
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"TheSavedState"];
}else {
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"TheSavedState"];
}
if(!savedState){
[wantThis setSelected:FALSE];
}else {
[wantThis setBackgroundImage:[UIImage imageNamed:@"wanted@2x.png"] forState:UIControlStateSelected];
[wantThis setSelected:TRUE];
wantThis.titleLabel.text = [NSString stringWithFormat:@"Wanted"];
}