我正在处理拖放应用,当用户放下图像时,我想在跳出的点上从中复制,然后原始的返回到它的初始点。 我决定在执行touchesEnded后将uiimageview添加到我的viewcontroller,
我有拖动视图类,其中包含方法:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
UIImageView *myimage;
myimage.image = self.image;
myimage.center = activePoint;
ViewController *cview ;
cview = [[ViewController alloc]init];
[cview getpoint: myimage];
}
现在在视图控制器中,这是getpoint选择器:
-(void) getpoint : (UIImageView *) mine{
UIImageView *newimage;
newimage = mine;
[self.view addSubview:newimage];
NSLog(@" in getpoint");
}
当我放下对象时,会出现此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
但是当我删除addsubview语句时,NSlog正确
任何解决方案?
答案 0 :(得分:0)
您实际上并没有为UIImageViews分配内存。这段代码很可疑:
CGPoint activePoint = [[touches anyObject] locationInView:self];
UIImageView *myimage;
myimage.image = self.image;
myimage.center = activePoint;
您需要分配/初始化图像视图
CGPoint activePoint = [[touches anyObject] locationInView:self];
UIImageView *myimage = [[UIImageView alloc] initWithFrame:rect];
myimage.image = self.image;
myimage.center = activePoint;
其中rect
变量包含您要添加到层次结构的图像视图的矩形尺寸。您无法将nil对象添加到NSArray。因此,由于UIImageView为nil,此调用将失败:
[self.view addSubview:newimage];