Iphone 从UILabel按钮获取UILabel的文本
我不熟悉iPhone 我的应用程序中有很多按钮,每个按钮上都有一个Iphone 从UILabel按钮获取UILabel的文本,iphone,ipad,uibutton,uilabel,buttonclick,Iphone,Ipad,Uibutton,Uilabel,Buttonclick,我不熟悉iPhone 我的应用程序中有很多按钮,每个按钮上都有一个UILabel,当用户单击任何按钮时,我想从该按钮获取该标签的相应文本 这是我的代码片段 - (void) buttonClicked:(UIButton*)sender { NSString *Txt= sender.titleLabel.text; NSLog(@"Txt=%@",Txt); } 但我的日志显示:Txt=null 任何帮助都将不胜感激。我想您可能已经在按钮上看到了如下标签: UILabe
UILabel
,当用户单击任何按钮时,我想从该按钮获取该标签的相应文本
这是我的代码片段
- (void) buttonClicked:(UIButton*)sender
{
NSString *Txt= sender.titleLabel.text;
NSLog(@"Txt=%@",Txt);
}
但我的日志显示:Txt=null
任何帮助都将不胜感激。我想您可能已经在按钮上看到了如下标签:
UILabel *lblText = [[UILabel alloc]initWithFrame:button.frame];
lblText.text = @"NameofButton";
[button addSubview:lblText];
[lblText release];
- (void) buttonClicked:(UIButton*)sender
{
//NSArray *arrSubViews = [sender subviews];
for (id anObject in [sender subviews]) {
if([anObject isKindOfClass: [UILabel class]]){
UIlabel *myLabel = anObject;
NSString *Txt= myLabel.text;
NSLog(@"Txt=%@",Txt);
}
}
}
现在,按钮单击事件将如下所示:
UILabel *lblText = [[UILabel alloc]initWithFrame:button.frame];
lblText.text = @"NameofButton";
[button addSubview:lblText];
[lblText release];
- (void) buttonClicked:(UIButton*)sender
{
//NSArray *arrSubViews = [sender subviews];
for (id anObject in [sender subviews]) {
if([anObject isKindOfClass: [UILabel class]]){
UIlabel *myLabel = anObject;
NSString *Txt= myLabel.text;
NSLog(@"Txt=%@",Txt);
}
}
}
确保您正在使用以下内容设置标题:
[button setTitle:@"your title" forState:UIControlStateNormal];
我认为没有必要使用uilabel