title: Write to plist when UIButton selected
body: <p>I'm making this code to set a default state for a UIButton in my DetailViewController, dependig on a strings value in a plist array with dictionaries. This works fine (in viewDidLoad). </p>
  
  <p>Then I want to write to the plist when favoriteButtonPressed, but this isn't working. I think something in the code is wrong. Can you help me with a correction?</p>
  
  <p>DetailViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface DetailViewController : UIViewController
  
  //The selectedObject property is recieved through segue from TableViewController
  @property (nonatomic, strong) NSMutableArray *selectedObject;
  
  @property (nonatomic, assign) BOOL favoriteButtonSelected;
  @property (weak, nonatomic) IBOutlet UIButton *favoriteButton;
  
  -(IBAction)favoriteButtonPressed:(id)sender;
  
  @end
  </code></pre>
  
  <p>DetailViewController.m</p>
  
  <pre><code>#import "DetailViewController.h"
  
  @interface DetailViewController ()
  
  @end
  
  @implementation DetailViewController
  
  @synthesize selectedObject;
  
  @synthesize favoriteButtonSelected;
  
  @synthesize favoriteButton;
  
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  
  if ([[selectedObject valueForKey:@"Favorite"] isEqual:@"Yes"])  {
  
      [favoriteButton setImage:[UIImage imageNamed:@"favoritedItem.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
      [favoriteButton setSelected:YES];
      [favoriteButton setHighlighted:YES];
      favoriteButtonSelected = 1;
  
  } else {
  
      [favoriteButton setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
      [favoriteButton setSelected:NO];
      [favoriteButton setHighlighted:NO];
      favoriteButtonSelected = 0;
  }
  }
  
  //Action to write to plist
  
  -(IBAction)favoriteButtonPressed:(id)sender
  {
  
  NSString *path = [[NSBundle mainBundle]
                    pathForResource:@"Objects" ofType:@"plist"];
  
  if (favoriteButtonSelected == 0) {
  
      [sender setSelected:YES];
      [sender setHighlighted:YES];
  
      favoriteButtonSelected = 1;
      [sender setImage:[UIImage imageNamed:@"favoritedItem.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
      [selectedObject setValue:@"Yes" forKey:@"Favorite"];
      [selectedObject writeToFile:path atomically:YES];
  
  } else {
  
      [sender setSelected:NO];
      [sender setHighlighted:NO];
  
      favoriteButtonSelected = 0;
      [sender setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
      [selectedObject setValue:@"No" forKey:@"Favorite"];
      [selectedObject writeToFile:path atomically:YES];
  }
  }
  
  @end
  </code></pre>