这就是我如何让我的 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?