在UIView中将UIImageView对象添加到NSMutableArray

时间:2011-11-16 23:14:05

标签: iphone ios arrays uiview nsmutablearray

我相信我可能正在处理一些不允许我检测并将UIImageView个对象添加到数组的视图问题。我真的可以使用一些建议。

我有一些UIImageViews图片与位于UIView之上的UIViewController相关联(已添加UIView以帮助{{ 1}}和一些额外的好处)。因此,当我触摸图像并“拖放”它时,我想在删除时将该图像添加到数组中。

我的drawRect获取了触摸的位置,并且正在触摸检查touchesBegan,然后按如下方式查看该视图:

UIImageView

然后,我使用-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:self]; //can't use self.view in a UIView, this may be causing issues? startLocation = location; // for reference as needed NSLog(@"Image x coord: %f", location.x); NSLog(@"Image y coord: %f", location.y); if ([touch view] == obiWan_Block) {obiWan_Block.center = location;} else if ([touch view] == r2d2_Block) {r2d2_Block.center = location;} else if ([touch view] == you_Block) {you_Block.center = location;} } 拖动UIImageView,最后使用touchesMoved'删除'图像。当touchesEnded在屏幕的某个区域下降时,我会将其“捕捉”到特定位置。在这一点上,我想把这个UIImageView放到一个数组中,但我没有运气。我相信我对所触及的各种观点以及通过UIImageView添加到我的addObject的内容感到困惑。或者,我可能完全错过了一些东西。这是我的NSMutableArray方法:

touchedEnded

我有-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:self]; UIView *temp = [touch view]; UIImageView *currentImageView = (UIImageView *)[touch view]; //DON'T THINK THIS IS RIGHT NSLog(@"Current Image View is: %@", currentImageView); if ((location.x > dropZone.origin.x) && (location.y >= dropZone.origin.y) && ([touch view] != NULL)) { CGRect frame = temp.frame; if (touchCount == 0) { frame.origin.x = 15; frame.origin.y = 180; temp.frame = frame; [quoteArray addObject: currentImageView]; touchCount++; } else if (touchCount == 1) { frame.origin.x = 70; frame.origin.y = 180; temp.frame = frame; [quoteArray addObject: currentImageView]; touchCount++; } .... 语句来检查NSLog是否正常工作:

addObject

日志总是说:

  

引用数组包含0个项目。内容=(null)

请告知并谢谢!

1 个答案:

答案 0 :(得分:1)

您的最后一部分代码显示您未初始化quoteArray。在创建代码时检查代码,我猜你在init方法中遗漏了一些东西。因为如果数组是正确的,那么NSLog应该显示如下:

NSArray *quoteArray = [NSArray array];
NSLog(@"%@", quoteArray); 
  

2011-11-17 17:37:00.506 TestApp [1382:207]()