iOS 3.1.3 中的 UIGestureRecognizer?
我正在努力使现有的 iPhone/iPad 项目向后兼容到 iPhoneOS 3.0。
我当前的测试设备是 iPod Touch,其版本为 3.1.3。
以下代码引发了问题:
Class gestureRecognizer = NSClassFromString(@"UISwipeGestureRecognizer");
if (gestureRecognizer != nil)
{
UISwipeGestureRecognizer * leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(didSwipeLeft:)];
leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipeRecognizer.delegate = self;
[self.view addGestureRecognizer:leftSwipeRecognizer];
_leftSwipeRecognizer = leftSwipeRecognizer;
}
根据 Apple 文档,UIGestureRecognizer 是从 iOS 3.2 开始定义的。因此,我希望在以前的操作系统版本上,ClassgestureReconizer
为nil
,并且跳过以下 if 。但它不会跳过。 gestureRecognizer
不是 nil
,if
内的代码开始执行并在 leftSwipeRecognizer.direction
处崩溃,因为:
-[UISwipeGestureRecognizer setDirection:]: unrecognized selector sent to instance 0x1e5720
这种情况相当混乱。我想我所做的一切都是照章办事。我尝试在使用某个类之前检查它是否存在,但是不应该存在的类却存在,欺骗了我的测试,不符合其预期规范,并且崩溃。
当然,我可以在各处进行一些 respondsToSelector
检查来解决此崩溃问题,但这不是一种优雅的方法。
还有其他建议吗?
I am working on making an existing iPhone/iPad project backwards compatible down to iPhoneOS 3.0.
My current test device is an iPod Touch with 3.1.3 on it.
The following bit of code is causing problems:
Class gestureRecognizer = NSClassFromString(@"UISwipeGestureRecognizer");
if (gestureRecognizer != nil)
{
UISwipeGestureRecognizer * leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(didSwipeLeft:)];
leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipeRecognizer.delegate = self;
[self.view addGestureRecognizer:leftSwipeRecognizer];
_leftSwipeRecognizer = leftSwipeRecognizer;
}
According to Apple documentation UIGestureRecognizer is defined starting from iOS 3.2. So I expect Class gestureReconizer
to be nil
on previous OS version and that the following if to be skipped. However it does not skip. gestureRecognizer
is not nil
, the code inside the if
starts executing and crashes at leftSwipeRecognizer.direction
because:
-[UISwipeGestureRecognizer setDirection:]: unrecognized selector sent to instance 0x1e5720
This situation is quite confusing. I guess I am doing everything by the book. I try to check if a class exists before I use it, however the class which shouldn't be there, is there, fools my test, does not comply with its expected specs, and crashes.
I could, of course put a few respondsToSelector
checks here and there to work around this crash, but it wouldn't be an elegant way to do it.
Any other suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 UIGestureRecognizer 类参考,在 "使用特殊注意事项",在检查该类是否存在后,您实际上确实需要进行额外的respondsToSelector检查。
这直接来自文档:
According to the UIGestureRecognizer class reference, under "Usage Special Considerations", you actually do need to do an additional respondsToSelector check after checking if the class exists.
This is directly from the documentation: