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 に答える