2014-12-07 88 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);