これが、お気に入りを管理するためのUIButtonの作成方法です。
-(IBAction)favoriteButtonPressed:(id)sender
{
if (favoriteButtonSelected == 0) {
[sender setSelected:YES];
favoriteButtonSelected = 1;
[sender setImage:[UIImage imageNamed:@"favoritedItem.png"]];
[selectedObject setValue:@"Yes" forKey:@"Favorite"];
} else {
[sender setSelected:NO];
favoriteButtonSelected = 0;
[sender setImage:[UIImage imageNamed:@"notFavorite.png"]];
[selectedObject setValue:@"No" forKey:@"Favorite"];
}
}
そしてこれはボタンのデフォルト状態を設定します:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[selectedObject valueForKey:@"Favorite"] isEqual:@"Yes"]) {
[favoriteButton setImage:[UIImage imageNamed:@"favoritedItem.png"]];
[favoriteButton setSelected:YES];
favoriteButtonSelected = 1;
} else {
[favoriteButton setImage:[UIImage imageNamed:@"notFavorite.png"]];
[favoriteButton setSelected:NO];
favoriteButtonSelected = 0;
}
}
そして私の質問は次のとおりです:favoriteButtonSelectedを宣言する方法は?そして、viewDidLoadでボタン(favoriteButton)とfavoriteButtonSelectedへの参照を作成する方法は?