目标C中选择器上的EXC_BAD_ACCESS

时间:2013-12-05 19:22:59

标签: ios objective-c uibutton

我有一个UIBarButtonItem类别,我使用自定义UIButton构建UIBarButtonItems,因为我发现UIButtons更容易自定义,然后是UIBarButtonItems。

现在,我想继续使用BarButtonItem的目标和操作属性,而不是使用按钮中的那些属性,以便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);
}];

1 个答案:

答案 0 :(得分:2)

您的代码是递归调用。

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

你设置如下:

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

itemUIBarButtonItem