0

当以这种方式按下 buttonCancel 时,我试图调用一个方法:

+ (DejalActivityView *)activityViewForView:(UIView *)addToView withLabel:(NSString *)labelText width:(NSUInteger)aLabelWidth;
{
// Immediately remove any existing activity view:
if (dejalActivityView)
    [self removeView];

// Remember the new view (so this is a singleton):
dejalActivityView = [[self alloc] initForView:addToView withLabel:labelText width:aLabelWidth];

buttonCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonCancel addTarget:self
                 action:@selector(callCancelAlert:)
       forControlEvents:UIControlEventTouchDown];
[buttonCancel setTitle:@"Cancel upload" forState:UIControlStateNormal];
buttonCancel.frame = CGRectMake(250, 560, 300, 40);
[addToView addSubview:buttonCancel];
[buttonCancel setImage:[UIImage imageNamed:@"socialize-navbar-bg.png"] forState:UIControlStateNormal];

return dejalActivityView;
}

和方法

 -(IBAction)callCancelAlert:(id)sender{

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: @"Announcement"
                      message: @"It turns out that you are playing Addicus!"
                      delegate: nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
[alert show];
//[alert release];
}

我收到此错误:

'NSInvalidArgumentException'的,原因是: '+ [DejalBezelActivityView callCancelAlert:]:无法识别的选择发送到类0x3184e4' *第一掷调用堆栈:(0x327862a3 0x3a61f97f 0x32789ca3 0x32788531 0x326dff68 0x346790c5 0x34679077 0x34679055 0x3467890b 0x3467859b 0x345a1523 0x3458e801 0x3458e11b 0x362805a3 0x362801d3 0x3275b173 0x3275b117 0x32759f99 0x326ccebd 0x326ccd49 0x3627f2eb 0x345e2301 0x39445 0x3aa56b20) libc++abi.dylib: 终止调用抛出异常

应用程序崩溃。

4

1 回答 1

3

您的第一个方法是类方法(+)而不是实例方法(-),因此您处于 Class 上下文中。

当你写

[buttonCancel addTarget:self
                 action:@selector(callCancelAlert:)
       forControlEvents:UIControlEventTouchDown];

self 不是指向类实例(例如 create by 和 alloc & init 消息),而是指向类本身。

因为在相同的方法中你分配一个实例,DejalActivityView你应该这样做:

[buttonCancel addTarget:dejalActivityView
                 action:@selector(callCancelAlert:)
       forControlEvents:UIControlEventTouchDown];
于 2013-03-11T18:54:34.203 回答