这是我的动作 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"];
}
}
如何在 viewDidLoad 中引用按钮?要使以下代码工作:
- (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;
}
}
编辑进展:
现在我这样做了:在助手编辑器中从 UIButton Ctrl 拖动到 ViewController。连接:Outlet,名称:FavoriteButton,类型:UIButton,存储:weak。但是错误仍然存在。+ viewDidUnload 中的合成错误和错误.. 建议?
View Controller 的 Assistant Editor 标头,通过 Ctrl 拖动添加了属性:
@interface DetailViewController : UIViewController {
IBOutlet UIScrollView *viewScroller;
}
@property (nonatomic, strong) IBOutlet UILabel *mylLabel;
@property (nonatomic, strong) NSString *selectedObj;
@property (strong, nonatomic) NSArray *detailsDataSource;
@property int detailIndex;
@property (weak, nonatomic) IBOutlet UIButton *favoriteButton; //The added property
@end