简体   繁体   English

目标C中选择器上的EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS on selector in objective C

I've got a UIBarButtonItem category where I build UIBarButtonItems with custom UIButtons, since I've found UIButtons easier to customize then UIBarButtonItems. 我有一个UIBarButtonItem类别,我用自定义UIButton构建UIBarButtonItems,因为我发现UIButtons更容易自定义,然后是UIBarButtonItems。

Now, I'd like to continue to use the BarButtonItem's target and action properties instead of using those in the button so that the BarButtonItem can continue to be customized externally without anyone having to know the implementation details (ie, that it is using a button internally). 现在,我想继续使用BarButtonItem的目标和操作属性,而不是使用按钮中的那些属性,以便BarButtonItem可以继续在外部进行自定义,而无需任何人知道实现细节(即,它正在使用按钮)内部)。

Now, in order to do that, I'm written up this code in my category: 现在,为了做到这一点,我在我的类别中写了这段代码:

+ (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];
}

Whenever the SD_executeBarButtonItemAction is called, I get a exc_bad_access on the selector, though I am not sure why. 每当调用SD_executeBarButtonItemAction时,我在选择器上得到一个exc_bad_access,虽然我不知道为什么。 Any ideas? 有任何想法吗? Is there a way around this? 有没有解决的办法?

Thanks! 谢谢!

EDIT: 编辑:

here is the code being called by that selector that is crashing: 这是崩溃的选择器调用的代码:

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);
}];

Your code is a recursive call. 您的代码是递归调用。

- (void)SD_executeBarButtonItemAction
{
    [self.target performSelector:self.action];
}

You set like: 你设置如下:

[close addTarget:item action:@selector(SD_executeBarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];

Where item is a UIBarButtonItem . 其中itemUIBarButtonItem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM