0

私は速くなります。6 つの画像があり、6 つのジェスチャと 1 つの IBAction が添付されています。各ジェスチャでパラメータをアクションに渡すようにしたいので、6 つの個別のアクションを記述する必要はありません。コードは次のとおりです。

    oneImage =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"one.gif"]];
    two Image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"two.gif"]];
    +4 more images

     UITapGestureRecognizer *oneGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
         UITapGestureRecognizer *twoGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
    +4 more gestures


    -(IBAction)insertChar:(id)sender
    {

    textfield.text = [textfield.text stringByAppendingString:@" PASS HERE VALUE FROM GESTURE,"ONE","TWO",etc "];
    }
4

2 に答える 2

1

メソッドに任意のデータを渡す方法はありませんinsertChar:senderがジェスチャ レコグナイザーになります。考えられる解決策の 1 つを次に示します。

oneImage =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"one.gif"]];
oneImage.tag = 1;
twoImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"two.gif"]];
twoImage.tag = 2;
// +4 more images

UITapGestureRecognizer *oneGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
UITapGestureRecognizer *twoGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
// +4 more gestures

-(IBAction)insertChar:(UITapGestureRecognizer *)sender {
    static NSString *labels[6] = { @"ONE", @"TWO", ... @"SIX" };
    UIView *view = sender.view;
    NSInteger tag = view.tag;
    NSString *label = labels[tag - 1]; // since tag is 1-based.

    textfield.text = [textfield.text stringByAppendingString:label];
}
于 2013-03-20T16:48:23.477 に答える
0

-(IBAction)insertChar:(id)senderパラメータとして取得した「送信者」の値を、作成したUIImageViewsに何らかの方法でリンクする必要があります。

アクション メソッドは次のようになります。

-(IBAction)insertChar:(id)sender
{

 UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer*)sender;
 UIView *view = gestureRecognizer.view;
//do whatever you want with the view that has the gestureRecgonizer's event on

}

次に、さまざまな方法でビューをリンクできます。1 つの方法は、タグ プロパティを使用することです。

于 2013-03-20T16:48:13.507 に答える