无法识别无法识别的选择器错误的原因
Unable to identify the cause of unrecognized selector error
所以我正在构建这个 iOS 应用程序。每当用户点击一个小方块时,该方块就会改变颜色。我已经确定我的所有方法都已定义。而且我已经更改了 xIndex 值的名称,以便我可以确保崩溃的根源不是视图的 .xIndex 值。但无论我似乎做什么,我似乎仍然得到:
'-[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
问题是您正在遍历 self.view
中的所有子视图,但并非所有这些视图都是 FunViewSquare
视图并且并非都响应 xIndex
。因此崩溃。
您似乎假设 for
循环只会挑选出 subviews
中的 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
对象。
所以我正在构建这个 iOS 应用程序。每当用户点击一个小方块时,该方块就会改变颜色。我已经确定我的所有方法都已定义。而且我已经更改了 xIndex 值的名称,以便我可以确保崩溃的根源不是视图的 .xIndex 值。但无论我似乎做什么,我似乎仍然得到:
'-[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
问题是您正在遍历 self.view
中的所有子视图,但并非所有这些视图都是 FunViewSquare
视图并且并非都响应 xIndex
。因此崩溃。
您似乎假设 for
循环只会挑选出 subviews
中的 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
对象。