1

我正在开发拖放应用程序,当用户放下图像时,我想从它的拖放点复制它,然后原始图像返回到它的初始点。我决定在执行 touchesEnded 后将 uiimageview 添加到我的视图控制器中,

我有包含该方法的拖动视图类:

 - (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 是正确的

任何解决方案?

4

1 回答 1

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];
于 2013-02-13T02:39:03.520 回答