Question: I have a custom UIButton on a view, when i press it, it changes state and img. The state of the UIButton stays when i exit the view and come back to it. But when i exit my app and start it again. The state of my custom UIButton is back to default. Is there a way I can save the state of that button when i exit the app?

here below is an example picture

[before http://i.minus.com/ibzHa1XCTDwSAJ.png](http://i.minus.com/ibzHa1XCTDwSAJ.png)

![after] <http://min.us/mtkB3JQRI>

and here is my code attempting to do that on -(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];
}

```

EDIT
----

```
  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"];
}

```

Answer: 
Use nsuserdefaults, so it will save as long as the user has your app. And you can overwrite it when needed.
It looks to me like you can just store the `selected` state as a `BOOL` and then read it back later. Look into `NSUserDefaults` to persist information like this across sessions. (The best way to do this for your particular app might be to store an `NSArray` containing identifiers for each of the events the user wants to participate in.)