0

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

4

1 回答 1

2

我开发了一个 UICheckbox 解决方案,它本质上只是一种切换。您可以轻松替换我的源项目中提供的图像并拥有您正在寻找的内容:

https://github.com/Brayden/UICheckbox

于 2012-08-09T21:08:20.360 回答