このコードを作成して、辞書を含む plist 配列の文字列値に依存して、DetailViewController の UIButton のデフォルト状態を設定しています。これは正常に機能します (viewDidLoad で)。
次に、favoriteButtonPressed のときに plist に書き込みたいのですが、これは機能しません。コードの何かが間違っていると思います。修正を手伝ってもらえますか?
DetailViewController.h
#import <UIKit/UIKit.h>
@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
DetailViewController.m
#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