Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 在选择器中将子视图添加到主uiview_Xcode_Drag And Drop_Addsubview - Fatal编程技术网

Xcode 在选择器中将子视图添加到主uiview

Xcode 在选择器中将子视图添加到主uiview,xcode,drag-and-drop,addsubview,Xcode,Drag And Drop,Addsubview,我正在使用拖放应用程序,当用户拖放图像时,我想在拖放点复制图像,然后原始图像返回到初始点。 在执行touchesEnded之后,我决定将uiimageview添加到我的viewcontroller中 我有一个包含以下方法的drag view类: - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint activePoint = [[touches anyObject] locationInView

我正在使用拖放应用程序,当用户拖放图像时,我想在拖放点复制图像,然后原始图像返回到初始点。 在执行touchesEnded之后,我决定将uiimageview添加到我的viewcontroller中

我有一个包含以下方法的drag view类:

 - (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运行正常


有什么解决办法吗

您实际上没有为UIImageView分配内存。此代码可疑:

CGPoint activePoint = [[touches anyObject] locationInView:self];
UIImageView *myimage;
myimage.image = self.image;
myimage.center = activePoint;
您需要alloc/init您的图像视图

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];

您是否为UIImageView initWithFrame:提供了大小合适的CGRect?你确定你传递的图像不是零吗?你能详细说明你说“它不起作用”的意思吗?