设置UIButton目标

时间:2013-03-11 18:44:51

标签: ios xcode

我试图以这种方式按下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:terminate调用抛出异常

应用程序崩溃。

1 个答案:

答案 0 :(得分:3)

你的第一个方法是类方法(+)而不是实例方法( - )因此你在类上下文中。

写作时

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

自我指向不是类实例(例如create by和alloc& init messages),而是指向类本身。

由于在同一方法中您分配了DejalActivityView的实例,您应该这样做:

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