草庐IT

ios - 无法为 UIButton 添加目标 - 无法识别的选择器发送到实例,尽管方法在同一个类中

coder 2023-09-29 原文

我在一个名为 StickerClass 的 Objective-C iOS 类中有一个 UIButton。该类有一个名为“theView”的 UIButton 的公共(public)实例。在 StickerClass 的构造函数中,我设置了初始属性,例如框架、图层属性和 subview ,以及点击时的目标,使用:

[theView addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];

有一个顶级类,称为 TopViewController,它创建 StickerClass 的实例,然后使用对变量 theView(StickerClass 实例中的 UIButton)的公共(public)访问,将按钮添加到 TopViewController(使用 [self.view addSubview:[myInstance theView]]).

回到 StickerClass,我有这个方法:

- (void)aMethod:(UIButton*)button {
    NSLog(@"Do some stuff...");
}

在 header 中,我在@end 之前有签名:

- (void)aMethod:(UIButton*)button;

但是当我点击按钮时,由于无法识别选择器,会抛出 SIGABRT 错误。错误是:

2014-09-06 21:13:23.448 Shy[6523:60b] -[UIControlTargetAction aMethod:]: unrecognized selector sent to instance 0x10922ca70
2014-09-06 21:13:23.450 Shy[6523:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControlTargetAction aMethod:]: unrecognized selector sent to instance 0x10922ca70'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101958495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016b799e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001019e965d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101949d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000101949938 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000100265f06 -[UIApplication sendAction:to:from:forEvent:] + 80
    6   UIKit                               0x0000000100265eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
    7   UIKit                               0x0000000100342880 -[UIControl _sendActionsForEvents:withEvent:] + 203
    8   UIKit                               0x0000000100341dc0 -[UIControl touchesEnded:withEvent:] + 530
    9   UIKit                               0x00000001005896f7 _UIGestureRecognizerUpdate + 5149
    10  UIKit                               0x000000010029ca15 -[UIWindow _sendGesturesForEvent:] + 928
    11  UIKit                               0x000000010029d6d4 -[UIWindow sendEvent:] + 909
    12  UIKit                               0x000000010027529a -[UIApplication sendEvent:] + 211
    13  UIKit                               0x0000000100262aed _UIApplicationHandleEventQueue + 9579
    14  CoreFoundation                      0x00000001018e7d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x00000001018e75f2 __CFRunLoopDoSources0 + 242
    16  CoreFoundation                      0x000000010190346f __CFRunLoopRun + 767
    17  CoreFoundation                      0x0000000101902d83 CFRunLoopRunSpecific + 467
    18  GraphicsServices                    0x0000000103acff04 GSEventRunModal + 161
    19  UIKit                               0x0000000100264e33 UIApplicationMain + 1010
    20  Shy                                 0x0000000100001c73 main + 115
    21  libdyld.dylib                       0x0000000101ff05fd start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有人知道为什么会这样吗?我想知道我是否错误地设置了目标,即因为它是自己,它试图在 TopViewController 中调用一个名为 aMethod 的方法而不是在 StickerClass 中调用(并通过将方法及其签名添加到 TopViewController 中进行测试,并使用类似的 NSLog - 这仍然抛出相同的错误)。非常感谢任何想法。

谢谢!

最佳答案

错误似乎源于在构造函数中使用 addTarget:action:forControlEvents:。如何创建一个单独的方法来将目标添加到 StickerClass 实例,

-(void)addButtonTarget {
    [self.theView addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
}

并在创建 StickerClass 实例后立即在 TopViewController 中调用此方法。

关于ios - 无法为 UIButton 添加目标 - 无法识别的选择器发送到实例,尽管方法在同一个类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25704405/

有关ios - 无法为 UIButton 添加目标 - 无法识别的选择器发送到实例,尽管方法在同一个类中的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  10. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

随机推荐