【发布时间】:2013-02-19 13:27:55
【问题描述】:
我正在开发一个项目,其中我使用 NSMutableArray 动态创建了一个 UIImageView
代码如下:
for (int i = 0; i < index ; i++) {
image_name= [NSString stringWithFormat:@"%@%dpng.png",[string_values objectAtIndex:0],i+1];
UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:image_name]];
tempImageView.userInteractionEnabled = YES;
CGSize size2=tempImageView.image.size;
int width2=size2.width/2;
int height2 =size2.height/2;
tempImageView.frame = CGRectMake(xvalue,yvalue,width2,height2);
[myArray addObject:tempImageView];
}
之后,我添加子视图以使用以下代码将其显示在屏幕上:
for(int i=0;i<[myArray count];i++) {
[self.view addSubview:[myArray objectAtIndex:i]];
}
以上所有代码都根据需要运行良好。现在对于 touchesBegan 方法,我正在执行以下代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
printf("the touch is fired");
for(int i=0;i<[myArray count];i++) {
[self.view addSubview:[myArray objectAtIndex:i]];
touchStart = [[touches anyObject] locationInView:[myArray objectAtIndex:i]];
}
}
当调用 touchesBegan 方法时,应用程序在打印 touchesBegan 方法的第一行之后立即崩溃。
我在这里遇到了困难,也经历了许多问题/答案,但找不到有效的解决方案。任何帮助将不胜感激。感谢您的好评
【问题讨论】:
-
声明 myArray 为 strong 并在初始化时使用 self.myArray = [NSMutableArray array];
-
stacktrace 会很有帮助。
-
不清楚你在touchesbegan函数里做什么?
-
你能告诉你想在 touchesBegan 方法中做什么@Tahir Mehmood
-
@Vinu1991 实际上在绘制图像之后>>我想拖动它们所有这些都只使用一个图像并且当我通过 NSMutable 数组添加 5 个图像时,应用程序实际上在 touchesBegan 方法中崩溃获取坐标和比较是在 touchesMoved 和 touchesEnded 方法中完成的>>但应用程序在转到它们之前会崩溃
标签: ios objective-c uiimageview nsmutablearray touchesbegan