【发布时间】:2010-01-26 22:14:46
【问题描述】:
我使用以下方法创建了 UIImageView 的子类(称为 MyPicture):
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"-- I AM TOUCHED --");
}
//…
当我使用带有 MyPicture 类的 InterfaceBuilder 创建 UIImageView 时,一切正常,控制台写道:“我被触摸了”。但仅当我在 IB 中激活用户交互时。
但是当我以编程方式创建 UIImageView 时,它确实有效。 在 ViewController.h 中:
MyButton * foo;
在 ViewController.m 中:
- (void)viewDidLoad {
flo = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
foo.userInteractionEnabled = YES;
foo.image = [UIImage imageNamed:@"Picture 1.png"];
[self.view addSubview:foo];
[super viewDidLoad];
}
我可以看到图片,所以它必须是我的 UIImageView 子类。但是为什么他没有检测到触摸呢?
【问题讨论】:
标签: iphone objective-c xcode