EXC_BAD_ACCESS 无法识别的选择器发送到实例
//adds a button to scroll list
-(void) addNode:(NSString *) atitle{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:atitle forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; // handle touch
[buttons addObject:btn];
....
}
//a data button was pressed
-(void) buttonTouched:(id)sender{
int index = [buttons indexOfObject:sender];
NSString *key = [sender titleLabel].text;
}
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
-(id) init{
self = [super init];
...
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ViewDidLoad" object:nil];
}
-(void) awakeFromNib{
[self loadData]; // loads some data
//calls addNode for a bunch of dynamic data
}
出现此错误:
2010-07-29 10:19:45.428 DataManager[2945:207] -[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x593abb0
2010-07-29 10:19:45.429 DataManager[2945:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x593abb0'
*** Call stack at first throw:
(
0 CoreFoundation 0x02399919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024e75de objc_exception_throw + 47
2 CoreFoundation 0x0239b42b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0230b116 ___forwarding___ + 966
4 CoreFoundation 0x0230acd2 _CF_forwarding_prep_0 + 50
5 UIKit 0x002bde14 -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x003476c8 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x00349b4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
8 UIKit 0x003486f7 -[UIControl touchesEnded:withEvent:] + 458
9 UIKit 0x00535070 _UIGestureRecognizerUpdateObserver + 3687
10 CoreFoundation 0x0237ad1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
11 CoreFoundation 0x0230f987 __CFRunLoopDoObservers + 295
12 CoreFoundation 0x022d8c17 __CFRunLoopRun + 1575
13 CoreFoundation 0x022d8280 CFRunLoopRunSpecific + 208
14 CoreFoundation 0x022d81a1 CFRunLoopRunInMode + 97
15 GraphicsServices 0x02bfe2c8 GSEventRunModal + 217
16 GraphicsServices 0x02bfe38d GSEventRun + 115
17 UIKit 0x002cbb58 UIApplicationMain + 1160
18 DataManager 0x00002940 main + 102
19 DataManager 0x000028d1 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
kill
kill
The Debugger has exited due to signal 15 (SIGTERM).The Debugger has exited due to signal 15 (SIGTERM).
//adds a button to scroll list
-(void) addNode:(NSString *) atitle{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:atitle forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; // handle touch
[buttons addObject:btn];
....
}
//a data button was pressed
-(void) buttonTouched:(id)sender{
int index = [buttons indexOfObject:sender];
NSString *key = [sender titleLabel].text;
}
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
-(id) init{
self = [super init];
...
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ViewDidLoad" object:nil];
}
-(void) awakeFromNib{
[self loadData]; // loads some data
//calls addNode for a bunch of dynamic data
}
Getting this error:
2010-07-29 10:19:45.428 DataManager[2945:207] -[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x593abb0
2010-07-29 10:19:45.429 DataManager[2945:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString buttonTouched:]: unrecognized selector sent to instance 0x593abb0'
*** Call stack at first throw:
(
0 CoreFoundation 0x02399919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024e75de objc_exception_throw + 47
2 CoreFoundation 0x0239b42b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0230b116 ___forwarding___ + 966
4 CoreFoundation 0x0230acd2 _CF_forwarding_prep_0 + 50
5 UIKit 0x002bde14 -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x003476c8 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x00349b4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
8 UIKit 0x003486f7 -[UIControl touchesEnded:withEvent:] + 458
9 UIKit 0x00535070 _UIGestureRecognizerUpdateObserver + 3687
10 CoreFoundation 0x0237ad1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
11 CoreFoundation 0x0230f987 __CFRunLoopDoObservers + 295
12 CoreFoundation 0x022d8c17 __CFRunLoopRun + 1575
13 CoreFoundation 0x022d8280 CFRunLoopRunSpecific + 208
14 CoreFoundation 0x022d81a1 CFRunLoopRunInMode + 97
15 GraphicsServices 0x02bfe2c8 GSEventRunModal + 217
16 GraphicsServices 0x02bfe38d GSEventRun + 115
17 UIKit 0x002cbb58 UIApplicationMain + 1160
18 DataManager 0x00002940 main + 102
19 DataManager 0x000028d1 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
kill
kill
The Debugger has exited due to signal 15 (SIGTERM).The Debugger has exited due to signal 15 (SIGTERM).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在将操作添加到按钮之前,我确实添加了
[self keep];
,现在它可以工作了!有什么理由吗?
self 是处理一切的
UIViewController
(至少我是这么认为的)I did add
[self retain];
before adding the action to the button and now it works!Any reason why?
self is the
UIViewController
that handles everything (at least I thought)如果您使用 ARC 编译并通过以下方式以编程方式设置选择器:
EXC_BAD_ACCESS 会发生在我身上,因为我创建了一个父视图,然后将子视图添加到另一个视图。子视图(带有按钮)有其父版本。一切看起来都很好,直到选择按钮然后崩溃。
选择器的任何异常很可能都是类发布问题。 Retain 不应该用作解决方法(当然,在 ARC 编译中,您不能使用保留)。应检查代码以查找将释放对象父对象的函数中可能存在的寡指针。
希望有帮助。
If you are using ARC compiles and programmatically setting the selector via:
the EXC_BAD_ACCESS occurs for me because I had created a parent and then added the subview to another view. The subview (with the button) had its parent release. Everything looked fine until the button was selected and then the crash.
Any exceptions with selectors are most probably class release issues. Retain should not be used as a work around (of course in ARC compiles you cannot use retain). Code should be review for possible widowed pointers in a function that will release the parent of the object.
Hope that helps.
buttons
是您初始化的数组吗(我假设您使用的是仅适用于数组的addObject
)?您说它应该将其添加到滚动列表中(我猜您的意思是滚动视图),但是您给出的错误使得您看起来像是将其传递到字符串对象中。要添加到滚动视图,您可以这样做:
另外,我没有看到在将对象添加到滚动视图时设置对象框架的任何地方。这是在将其添加为子视图之前需要执行的操作。
对我来说效果很好(注意,你应该在某个地方初始化并释放可变数组,我只是很快就做到了):
控制台输出= 2010-07-29 15:54:54.563 Torch [52222:207]是的嗨和0
代码我用过的:
(...)
Is
buttons
an array that you initialized (I'm assuming since you are usingaddObject
which is only for arrays)? You say that it's supposed to add it to a scroll list (i'm guessing you meant a scrollview), but the error you are giving makes it seem like you are passing it into a string object.To add to a scrollview, you would do:
Also, I don't see anywhere that you set the frame of the object for when it gets added to the scrollview. That's something you would need to do before adding it as a subview.
Works just fine for me (note, you should be initializing and releasing the mutable array somewhere, I just did this quickly):
Console output = 2010-07-29 15:54:54.563 Torch[52222:207] Yep Hi and 0
Code that I used:
(...)