2013-03-11 118 views
0

我想,當以這種方式被壓buttonCancel調用一個方法:設置UIButton的目標

+ (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' * F IRST扔調用堆棧: (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:終止叫做拋出異常

而且該應用程序崩潰。

+0

是'buttonCancel'a伊娃? – limon 2013-03-11 18:48:59

+0

沒有它的UIButton * buttonCancel;並在第一個報告的方法之前創建 – Alessandro 2013-03-11 18:50:11

回答

3

您的第一個方法是類方法(+)而不是實例方法( - ),因此您處於Class上下文中。

當你寫

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

自點而不是一個類的實例(例如通過創建或Alloc &初始化消息),但類本身。

由於在相同的方法,你的Alloc的DejalActivityView一個實例,你應該這樣做:

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