我正在使用来自这里的一些代码来确定何时多点触摸序列中的最后一个手指已经解除。
下面是代码:
/*
Determining when the last finger in a multi-touch sequence has lifted
When you want to know when the last finger in a multi-touch sequence is lifted
from a view, compare the number of UITouch objects in the passed in set with the
number of touches for the view maintained by the passed-in UIEvent object.
For example:
*/
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if ([touches count] == [[event touchesForView:self] count]) {
// last finger has lifted....
}
}我收到警告了:
passing argument 1 of 'touchesForView:' from distinct Objective-C type
代码构建并运行良好,但我想删除它,但不明白警告意味着什么。有什么想法吗?
发布于 2009-07-03 13:46:28
当您提供一个与预期不同的类型的对象时,就会出现这个特别的警告。
在这种情况下,touchesForView:需要一个UIView对象,但是您要传递给它一个无论self类型在此代码中是什么类型的对象。
为了消除警告,可以传递正确类型的对象,也可以强制编译器将self指针转换为正确的类型:
if ([touches count] == [[event touchesForView:(UIView *)self] count])但是,请注意,如果self的行为不像UIView,那么您可能会为自己设置一些细微的错误。
更新:
我做了一个快速搜索,发现了这篇文章,它有一些很好的指南来处理可可警告,以及它们的共同原因。
基于这些信息,我想快速列出您发布的代码应该发生什么。我假设您使用Xcode中的模板创建了一个新的iPhone应用程序,并且应用程序只有一个UIView (如Interface中所示)。
要使用您发布的代码,您需要创建一个定制的UIView子类,如下所示:
// YourViewClass.h:
@interface YourViewClass : UIView // Note #1
{
}
@end
// YourViewClass.m:
@import "YourViewClass.h" // Note #2
@implementation YourViewClass
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
if ([touches count] == [[event touchesForView:self] count])
{
// last finger has lifted....
}
}
@end在Interface中,您可以将view对象的类型设置为YourViewClass,然后就可以了。
有了我前面所示的代码,您就不应该收到警告。这使我认为上述步骤中的一个没有得到适当的执行。首先,要确保:
self对象实际上是一个UIView子类(注1)#import类的头(注2)https://stackoverflow.com/questions/1079380
复制相似问题