【发布时间】:2013-12-05 19:22:59
【问题描述】:
我有一个 UIBarButtonItem 类别,我在其中使用自定义 UIButtons 构建 UIBarButtonItems,因为我发现 UIButtons 比 UIBarButtonItems 更容易自定义。
现在,我想继续使用 BarButtonItem 的 target 和 action 属性,而不是使用按钮中的属性,以便 BarButtonItem 可以继续在外部自定义,而无需任何人知道实现细节(即,它是在内部使用按钮)。
现在,为了做到这一点,我在我的类别中编写了这段代码:
+ (UIBarButtonItem *)backBarButtonItemWithColor:(UIColor *)color
{
UIImage *closeIcon = [MyImageUtility navBarBackArrow];
if (color) closeIcon = [closeIcon imageWithColorOverlay:color];
UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, closeIcon.size.width+10.0f, closeIcon.size.height+10.0f)];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:close];
[close setImage:closeIcon forState:UIControlStateNormal];
[close addTarget:item action:@selector(SD_executeBarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
return item;
}
- (void)SD_executeBarButtonItemAction
{
[self.target performSelector:self.action];
}
每当调用 SD_executeBarButtonItemAction 时,我都会在选择器上获得 exc_bad_access,但我不确定为什么。有任何想法吗?有没有办法解决这个问题?
谢谢!
编辑:
这是该选择器正在崩溃的调用代码:
void (^transition)(void) = ^(void) {
[self.rightContainer setFrame:[self offscreenContainerFrame]];
[self.centerContainer setAlpha:1.0f]; //TODO: this is unreliable in iOS6 -- we should add a view to the top of it to darken
[self.centerContainer setTransform:CGAffineTransformIdentity];
};
[self notifyWillShowPrimaryViewController];
[self performBlock:transition animated:YES completion:^(BOOL finished) {
[self notifyDidShowPrimaryViewController];
[self setForegroundController:self.primaryNavigationController];
if (block != NULL) block(finished);
}];
【问题讨论】:
-
self.action是什么? -
UIBarButtonItem的目标和动作在哪里设置? -
UIBarButtonItem的action属性
-
它们被设置为 [back setTarget:self];和 [返回 setAction:@selector(showPrimaryAction:)];其中 self 是一个 viewController
-
是否有可能在点击条形按钮时,条形按钮的目标已被释放?
标签: ios objective-c uibutton