当前位置:  开发笔记 > IOS > 正文

在事件处理程序中获取UIButton的标题

如何解决《在事件处理程序中获取UIButton的标题》经验,为你挑选了2个好方法。

我创建一个按钮并将标题设置为"单击此处".当我按下该按钮时,我想获得该按钮标题并记录下来.这是我的代码,我哪里错了?

-(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.
}

Harry Lachen.. 72

你的displayvalue:方法应该是这样的:

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

(请查看XCode中的文档,它会给你正确的答案.)



1> Harry Lachen..:

你的displayvalue:方法应该是这样的:

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

(请查看XCode中的文档,它会给你正确的答案.)



2> Vaibhav Sara..:
-(void)displayvalue:(id)sender 
{
    UIButton *resultebutton= (UIButton*)sender;
    NSLog(@"The button title is %@ ", resultebutton.titleLabel.text);
}

推荐阅读
贾志军
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有