Alessandro
Alessandro

Reputation: 4100

Get UIButton title from UIPanGestureRecogniser

I have assigned a pan gesture recogniser to my UIButton, but I don't seem to be able to get the titleLabel of the button. I have tried this:

-(void)move:(id)sender{
[(UITapGestureRecognizer*)sender view].titleLabel.text

and

UIButton *resultebutton= (UIButton*)sender;

But I don't seem to get it. The app crashes with both of these, and the first gives an error

Upvotes: 0

Views: 74

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130193

You should retrieve this info in the method that handles your gesture recognizer. That way, you can access the gestures view, cast it as UIButton, and then extract the text from the title label of the button.

- (void)move:(id)sender
{
    UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;

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

    NSString *title = button.titleLabel.text;
}

The problem with the code you've tried is that in the first case, the code wouldn't even compile because you're not casting view to UIButton in anyway, and UIView doesn't declare a property called titleLabel. The second is casting UIPanGestureRecognizer to UIButton, which couldn't ever work.

Upvotes: 2

Related Questions