我有一个奇怪的问题,UITapGestureRecognizer
当要调用它的操作时,我总是以崩溃或抛出异常(无法识别的选择器)告终。我以前使用过手势识别器,但这次我将它附加到 aUIView
上,它是 an 的一个属性NSObject
(并且我正在使用对象的方法来构造视图)。以下是我正在做的一个最小示例:
我的对象.h:
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@property (nonatomic, strong) UIView *aView;
- (void)createViewWithFrame:(CGRect)frame;
@end
我的对象.m:
@interface MyObject ()
- (void)tapped:(id)sender; // Non-specific id type for brevity
@end
@implementation MyObject
- (void)tapped:(id)sender {
NSLog(@"Tapped");
}
- (void)createViewWithFrame:(CGRect)frame {
_aView = [[UIView alloc] initWithFrame:frame];
self.aView.backgroundColor = [UIColor blueColor];
self.aView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapped:)];
[self.aView addGestureRecognizer:tap];
}
视图控制器.m:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
MyObject *obj = [[MyObject alloc] init];
[obj createViewWithFrame:self.view.frame]; // Excuse poor frame - example only
[self.view addSubview:obj.aView];
}
然后,当我点击视图时,应用程序在此处崩溃:
libobjc.A.dylib`objc_msgSend:
0x10e008c: movl 8(%esp), %ecx
0x10e0090: movl 4(%esp), %eax
0x10e0094: testl %eax, %eax
0x10e0096: je 0x10e00e8 ; objc_msgSend + 92
0x10e0098: movl (%eax), %edx
0x10e009a: pushl %edi
0x10e009b: movl 8(%edx), %edi ; Thread 1: EXC_BAD_ACCESS (code=2, address=0x9)
0x10e009e: pushl %esi
0x10e009f: movl (%edi), %esi
0x10e00a1: movl %ecx, %edx
确切的崩溃点各不相同,我偶尔会收到无效的选择器错误。的NSLog
,自然,永远不会发生。我之前(成功地)使用过手势识别器,这让我相信问题出在我的设置/设计中。谁能解释为什么手势识别器在这种情况下不能正常工作?