문제

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