質問

In my app i am adding uibuttons in uiscrollview as subviews. I want to get tags of the buttons on long press. My code is

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
longPress.delegate = self;
[gridScrollView addGestureRecognizer:longPress];

I want to know that how can I get the particular button tag in this code

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {

}

役に立ちましたか?

解決

You will get all the buttons from the subviews property

for (UIButton *eachBtn  in [gridScrollView subviews]) {
    if ([eachBtn isKindOfClass:[UIButton class]]) {
         NSLog(@"Button Tag : %i",eachBtn.tag);
    }
} 

If you want to pass a button's tag then set the same tag to gesture

longPress.view.tag = btn.tag

After that In your long press handler method take this tag

  NSLog(@"Button Tag : %i",longPress.view.tag);

他のヒント

If you don't keep track of all the buttons added to the scroll view (e.g. by adding them into array), just enumerate all the subviews of the scroll view, check that current subview is a UIButton and get subview.tag

for (UIView *subview in scrollView.subviews) {
    if ([subview isKindOfClass:[UIButton class]]) {
        // subview.tag …
    }
}

or, if you know the tag, use -[scrollView viewWithTag:]

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top