ios - 设置 UIButton 目标

标签 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 0x 3458e801 0x3458e11b 0x362805a3 0x362801d3 0x3275b173 0x3275b117 0x32759f99 0x326ccebd 0x326ccd49 0x3627f2eb 0x345e2301 0x39445 0x3aa56b20) libc++abi.dylib:终止调用抛出异常

应用程序崩溃了。

最佳答案

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

当你写作时

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

self 不是指向一个类实例(例如,通过消息创建、分配和初始化消息)而是指向类本身。

因为在相同的方法中您分配了 DejalActivityView 的实例,所以您应该这样做:

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

关于ios - 设置 UIButton 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346142/

相关文章:

ios - UILabel 运行超过屏幕大小

ios - iOS 7 中的 UIActivityViewController 缺少选项

android - 在iOS和Android上使用带有混合语言文本的自定义字体

ios - 如何在我的导航 Controller 中将工具栏按钮添加到工具栏

ios - 在 Swift 中将变量传递回父级

iphone - CTFontRef 存在问题

ios - 如何使用 StoryBoard 在 UITabBarController 中设置选定的选项卡?

xcode - 具有 LeftTop 圆角和 RightTop 角角的 MapView

swift - 类型 'StorageMetadata' 的值没有成员 'downloadURL'

html - 在 Swift 中解码 HTML 会在 iOS 设备上导致 BAD_ACCESS