【问题标题】:Dynamic Button Problem: unrecognized selector sent to instance动态按钮问题:发送到实例的选择器无法识别
【发布时间】:2010-07-29 03:50:43
【问题描述】:

我正在 iPhone 应用上动态创建按钮。


UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setTitle:atitle forState:UIControlStateNormal];

[btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; // handle touch

[buttons addObject:btn];


----------

-(void) buttonTouched:sender{
}

----------

-[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x592ef70
2010-07-28 08:59:43.551 DataManager[1707:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x592ef70'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02398919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x024e65de objc_exception_throw + 47
    2   CoreFoundation                      0x0239a42b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x0230a116 ___forwarding___ + 966
    4   CoreFoundation                      0x02309cd2 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x002bce14 -[UIApplication sendAction:to:from:forEvent:] + 119
    6   UIKit                               0x003466c8 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x00348b4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    8   UIKit                               0x003476f7 -[UIControl touchesEnded:withEvent:] + 458
    9   UIKit                               0x00534070 _UIGestureRecognizerUpdateObserver + 3687
    10  CoreFoundation                      0x02379d1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    11  CoreFoundation                      0x0230e987 __CFRunLoopDoObservers + 295
    12  CoreFoundation                      0x022d7c17 __CFRunLoopRun + 1575
    13  CoreFoundation                      0x022d7280 CFRunLoopRunSpecific + 208
    14  CoreFoundation                      0x022d71a1 CFRunLoopRunInMode + 97
    15  GraphicsServices                    0x02bfd2c8 GSEventRunModal + 217
    16  GraphicsServices                    0x02bfd38d GSEventRun + 115
    17  UIKit                               0x002cab58 UIApplicationMain + 1160
    18  DataManager                         0x00001c4c main + 102
    19  DataManager                         0x00001bdd start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

有什么想法吗?

【问题讨论】:

  • 将 .h 和 .m 更改为 -(void)buttonTouched:(id)sender { } 现在我得到:wait_fences: failed to receive reply: 10004003 Program received signal: "EXC_BAD_ACCESS"。

标签: iphone uibutton unrecognized-selector


【解决方案1】:

因为您在 addTarget 的选择器参数中包含了一个冒号 (:),所以接收选择器必须接受一个参数。运行时无法识别选择器@selector(buttonTouched:),因为没有具有该名称的方法可以接受参数。更改方法签名以接受 id 类型的参数以解决此问题。

【讨论】:

  • 将 .h 和 .m 更改为 -(void)buttonTouched:(id)sender { } 现在我得到:wait_fences: failed to receive reply: 10004003 Program received signal: "EXC_BAD_ACCESS"。有什么想法吗?
【解决方案2】:

应该是:

-(void)buttonTouched:(id)sender { }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2013-01-23
    • 2023-03-14
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多