2014-12-07 71 views
-1

如何在长按手势发送按钮名称,标签或文字?我需要知道长久以来的按钮名称。长按手势发送按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    [button setTagS:[[ToalData objectAtIndex:i] objectAtIndex:4]]; 
    [button addTarget:self action:@selector(siteButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    ///For long// 
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] 
               initWithTarget:self 
               action:@selector(handleLongPress:)]; 
    longPress.minimumPressDuration = 1.0; 
    [button addGestureRecognizer:longPress]; 

    //// 


    NSString *string1 = [NSString stringWithFormat:@"%@", [[ToalData objectAtIndex:i] objectAtIndex:1]]; 
    [button setTitle:string1 forState:UIControlStateNormal]; 
    button.frame = CGRectMake(XLocatioan, YLocation, 90, 30); 

    [self.view addSubview:button]; 


(void)handleLongPress:(UILongPressGestureRecognizer*)sender 
{ 

if (sender.state == UIGestureRecognizerStateEnded) 
{ 
    NSLog(@"UIGestureRecognizerStateEnded"); 

} 
else if (sender.state == UIGestureRecognizerStateBegan){ 
    NSLog(@"UIGestureRecognizerStateBegan"); 




     } 

}

回答

3

每个手势识别附着于"view",这是对手势识别的属性。

所以在你的 “handleLongPress” 的方法,你可以这样做:

UIButton *button = (UIButton *)sender.view; 

NSLog(@"title is %@", button.titleLabel.text); 
NSLog(@"tag is %d", button.tag);