Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting title of UIButton in event handler

I create a button and set title as "Click here". When I press that button I want to get that button title and log it. Here's my code, where am I going wrong?

-(void)clicketbutton {
    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [mybutton setTitle:@"Click here"  forState:UIControlStateNormal];
    [mybutton addTarget:self  
       action:@selector(displayvalue:)forControlEvents:UIControlEventTouchUpInside]; 
}

-(void)displayvalue:(id)sender {       
    UIButton *resultebutton= [UIButton buttonWithType:UIButtonTypeCustom];

    resultebutton=sender;// pls clear here.. my question here , it it possible or not. if possible how ?
    NSLog(@" The buttontitile is %@ ", [resultebutton.Title] // here also.
}
like image 562
Raju Avatar asked Sep 10 '25 04:09

Raju


2 Answers

Your displayvalue: method should look something like this:

-(void)displayvalue:(id)sender {       
    UIButton *resultButton = (UIButton *)sender;
    NSLog(@" The button's title is %@.",  resultButton.currentTitle);
}

(Please check out the documentation in XCode, it would have given you the right answer.)

like image 121
Harry Lachenmayer Avatar answered Sep 12 '25 17:09

Harry Lachenmayer


-(void)displayvalue:(id)sender 
{
    UIButton *resultebutton= (UIButton*)sender;
    NSLog(@"The button title is %@ ", resultebutton.titleLabel.text);
}
like image 30
Vaibhav Saran Avatar answered Sep 12 '25 16:09

Vaibhav Saran