Xavi Valero
Xavi Valero

Reputation: 2057

Getting buttons title in its click event

                for (int i = 0; i < ABMultiValueGetCount(emails)  ; i++)
                        {

                            CFStringRef email = ABMultiValueCopyValueAtIndex(emails, i);
                            //CFStringRef emailType = ABMultiValueCopyLabelAtIndex(emails, i);
                            emailString  = (NSString *)email;
                            //emailTypeString = (NSString *)emailType;

                            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                            button.frame = CGRectMake(15, 320 - x, 290, 40);
                            [button setTitle:emailString forState:UIControlStateNormal];
                            button.titleLabel.font  = [UIFont fontWithName:@"Helvetica Bold" size:18];
                            [button addTarget:self action:@selector(OnEmailIdSelection:) forControlEvents:UIControlEventTouchUpInside];
                            [self.selectEmailId addSubview:button];
                            x = x+70;
                        }


                }

I have the above code in which I have a button click event OnEmailIdSelection. Inside this event, I want to get the sender's, that is, clicked button's title into a string variable.

Upvotes: 0

Views: 861

Answers (2)

Aliaksandr Andrashuk
Aliaksandr Andrashuk

Reputation: 965

-(void) OnEmailIdSelection:(id)sender {
    UIButton* button = (UIButton*)sender;
    NSString* title = [button titleForState: UIControlStateNormal];
}

Upvotes: 1

Simon
Simon

Reputation: 25983

Try this:

-(void) OnEmailIdSelection:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton*)sender;
        NSString *title = button.currentTitle;

        // do whatever you want with title
    }
}

Upvotes: 2

Related Questions