Xcode 在选择器中将子视图添加到主uiview
我正在使用拖放应用程序,当用户拖放图像时,我想在拖放点复制图像,然后原始图像返回到初始点。 在执行touchesEnded之后,我决定将uiimageview添加到我的viewcontroller中 我有一个包含以下方法的drag view类: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
- (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?你确定你传递的图像不是零吗?你能详细说明你说“它不起作用”的意思吗?