我创建了以下类来获取带有彩色图像的工具栏
@interface likeUIBarButtonItem : UIBarButtonItem
- (id) initWithStatus: (enum ApartmentLikeStatus)p_status;
@end
m 文件如下所示:
- (IBAction) likeStatus_Clicked: (id) sender
{
self.numberOfClicks++;
[self.statusButton setImage:[self getStatusImage] forState:UIControlStateNormal];
}
- (id) initWithStatus: (enum ApartmentLikeStatus)p_status
{
self.statusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.statusButton setImage:[self getStatusImage] forState:UIControlStateNormal];
[self.statusButton addTarget:self action:@selector(likeStatus_Clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.statusButton setFrame:CGRectMake(0, 0, 20, 20)];
self = [[likeUIBarButtonItem alloc] initWithCustomView:self.statusButton];
return self;
}
在视图控制器中,我执行以下操作:
likeUIBarButtonItem* stausBtn = [[likeUIBarButtonItem alloc] initWithStatus:self.apartmentToShow.userLikness];
// List of toolbaritems
NSArray* toolbarItems = [NSArray arrayWithObjects: stausBtn, nil];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
该按钮被显示,但点击它时我得到一个汇编代码
0x120a09f: movl (%edi), %esi <-- thread 1: EXC_BAD_ACCESS (code=2 address=0x0)
虽然我的 statusButton 被声明了,但(strong, nonatomic)
我猜它在 init 和函数调用之间的某个地方被释放,是这种情况还是其他原因?不管它是什么,我该如何解决?