Ios 无法确定无法识别的选择器错误的原因
所以我正在构建这个iOS应用程序。每次使用鼠标点击一个小正方形时,该正方形应该会改变颜色。我已经确定我所有的方法都已定义。我已经更改了xIndex值的名称,以便确保崩溃的根源不是视图的.xIndex值。但无论我做了什么,我似乎仍然得到:Ios 无法确定无法识别的选择器错误的原因,ios,objective-c,iphone,uiview,unrecognized-selector,Ios,Objective C,Iphone,Uiview,Unrecognized Selector,所以我正在构建这个iOS应用程序。每次使用鼠标点击一个小正方形时,该正方形应该会改变颜色。我已经确定我所有的方法都已定义。我已经更改了xIndex值的名称,以便确保崩溃的根源不是视图的.xIndex值。但无论我做了什么,我似乎仍然得到: '-[UIView xIndex]: unrecognized selector sent to instance 0x7fe06be6ce70' 请注意,上面的实例引用specificSquare的内存地址。猜猜看?我们为此奋斗了几个小时 - (void)h
'-[UIView xIndex]: unrecognized selector sent to instance 0x7fe06be6ce70'
请注意,上面的实例引用specificSquare的内存地址。猜猜看?我们为此奋斗了几个小时
- (void)handleFunViewSquareTap:(UITapGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
// handling code
CGPoint locationOfTap = [sender locationInView: self.view];
NSInteger xVIndex = floorf(locationOfTap.x / 40.f);
NSInteger yIndex = floorf(locationOfTap.y / 40.f);
// find the view that matches these indexes
for(FunViewSquare *specificSquare in [self.view subviews])
{
if((specificSquare.xIndex == xVIndex) && (specificSquare.yIndex == yIndex))
{
// //specificSquare.backgroundColor = [ViewController randomColor];
}
}
}
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frameRect = self.view.frame;
NSInteger xIndex, yIndex = 0;
for( CGFloat yPosition = 0.0; yPosition < frameRect.size.height; yPosition+=40.0f )
{
// reset xIndex on every iteration
xIndex = 0;
for( CGFloat xPosition = 0.0; xPosition < frameRect.size.width; xPosition+=40.0f )
{
FunViewSquare *randomSquare = [[FunViewSquare alloc] initWithFrame: CGRectMake(xPosition, yPosition, 40.f, 40.0f)];
if(randomSquare)
{
randomSquare.backgroundColor = [ViewController randomColor];
randomSquare.xIndex = xIndex;
randomSquare.yIndex = yIndex;
[self.view addSubview: randomSquare];
}
xIndex++;
}
yIndex++;
}
butGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleFunViewSquareTap:)];
if(butGestureRecognizer)
{
[self.view addGestureRecognizer: butGestureRecognizer];
}
}
+ (UIColor *)randomColor
{
UIColor *colorToReturn;
uint32_t randomNumber = random();
randomNumber = (randomNumber % 10); // a random number between 0 & 10
switch(randomNumber)
{
case 0 :
colorToReturn = [UIColor blueColor];
break;
case 1 :
colorToReturn = [UIColor grayColor];
break;
case 2 :
colorToReturn = [UIColor greenColor];
break;
case 3 :
colorToReturn = [UIColor purpleColor];
break;
case 4 :
colorToReturn = [UIColor redColor];
break;
case 5 :
colorToReturn = [UIColor brownColor];
break;
case 6 :
colorToReturn = [UIColor cyanColor];
break;
case 7 :
colorToReturn = [UIColor orangeColor];
break;
case 8 :
colorToReturn = [UIColor magentaColor];
break;
case 9 :
colorToReturn = [UIColor whiteColor];
break;
default :
colorToReturn = [UIColor blackColor];
}
return(colorToReturn);
}
@end
-(void)handleFunViewSquareTap:(UITapGestureRecognitor*)发送方
{
if(sender.state==UIGestureRecognitizerStateEnded)
{
//处理代码
CGPoint locationOfTap=[sender locationInView:self.view];
NSInteger xVIndex=地板(顶部x/40.f的位置);
NSInteger yIndex=地板(顶部y/40.f的位置);
//查找与这些索引匹配的视图
用于(在[self.view子视图]中的FunViewSquare*specificSquare)
{
if((specificSquare.xIndex==xvidex)和&(specificSquare.yIndex==yIndex))
{
////specificSquare.backgroundColor=[ViewController randomColor];
}
}
}
}
-(无效)viewDidLoad{
[超级视图下载];
CGRect frameRect=self.view.frame;
NSInteger xIndex,yIndex=0;
对于(CGFloat yPosition=0.0;yPosition
问题在于,您正在迭代self.view
中的所有子视图,而不是所有这些视图都是FunViewSquare
视图,并且不都响应xIndex
。因此坠机
您似乎假设for
循环只会选择子视图中的FunViewSquare
对象,但事实并非如此
您需要在此处进行一些自省-按如下方式重写代码:
for(FunViewSquare *specificSquare in [self.view subviews]) {
if ([specificSquare isKindOfClass:[FunViewSquare class]](
if ((specificSquare.xIndex == xVIndex) && (specificSquare.yIndex == yIndex)){
//specificSquare.backgroundColor = [ViewController randomColor];
}
}
}
通过这种方式,您可以检查以确保specificSquare
确实是FunViewSquare
对象。如果您可以轻松重现此崩溃。用调试器一步一步地调试代码-点击方框并跟随代码直到代码崩溃。是。它在if((specificSquare.xIndex…)行崩溃。我试图在if条件下注释掉该操作,但没有效果。很抱歉,我忽略了提及这一点。问题很清楚。self.view
的子视图并非都是您的FunViewSquare
类的实例。有些是普通的UIView
实例。(MyType*myArray中的obj)< /代码>不过滤数组,只对代码的对象进行循环> MyType < /代码>。您必须自己完成。严格地说,将每个<代码>特定的代码> <代码> > <代码> FunVIEWSWORD < /代码>是不正确的:您应该考虑将循环对象的类型更改为<代码> UIVIEW/CODE >,然后在确定类型之后重新分配。