【发布时间】:2014-10-28 12:07:30
【问题描述】:
我创建了一个继承 UIView 类的 UIElement 类,以便为我的视图创建自定义属性。
我遇到的问题,正如您所看到的代码,我无法将 UIElement 对象分配给 Gesture Recognizer 的 view 属性。
只要 UIElement 是 UIView 的子元素,我就不能建立子父关系。
我正在考虑(很多)想出一个解决方案,但我没有找到任何解决方案。
- (void)viewDidLoad {
[super viewDidLoad];
UIElement * element = [[UIElement alloc] init];
element.name = @"A Name"; // Custom property
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(responsToGesture:)];
longPress.minimumPressDuration = 0.3;
[longPress setDelegate:self];
[element addGestureRecognizer:longPress];
}
- (void)responsToGesture:(UILongPressGestureRecognizer *)sender {
UIElement * element = sender.view; // Here's the problem: Incompatible pointer types
NSLog(@"Element's Name: %@", element.name);
}
【问题讨论】:
-
您必须将手势识别器添加到您的 uielement。
-
在
–viewDidLoad超出其范围后,谁让您的element或longPress存活?我猜你忘了让你的对象活着...... -
对不起,我已经编辑了代码。
-
阅读 C 中的“cast”。
-
好吧,我咬一口。什么是 UIElement?那是可可触摸课程吗?我在任何地方都找不到它。是第三方类吗?如果是这样,它是 UIView 的子类吗? Objective-C 没有多重继承,所以如果 UIElement 不是 UIView 的子类,就不能让它成为 UIView 的子类。
标签: ios objective-c uiview customization uigesturerecognizer