单击时更改 UIButton 文本
Posted
技术标签:
【中文标题】单击时更改 UIButton 文本【英文标题】:Change UIButton text when clicked 【发布时间】:2013-09-03 04:03:05 【问题描述】:我的程序执行 buttonPressed 方法并执行 if else 语句,但我的按钮上的文本仍然没有改变。有什么我错过的吗?我什至已经在 xib 中对其进行了自定义,但是即使进入其他语句,buttonPlayMusic 的文本仍保持为“播放”。
我想我也连接了按钮。
[viewDidLoad]
self.buttonPlayMusic = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buttonPlayMusic.frame = CGRectMake(((scrollView.frame.size.width - 200) / 2) + cx, 360, 200, 50);
[self.buttonPlayMusic setTitle:@"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.toggleButonPlayMusic = YES; //YES to play, NO to stop music
[scrollView addSubview:self.buttonPlayMusic];
-(IBAction)buttonPressed:(id)sender
NSLog(@"SSS");
if (self.toggleButonPlayMusic == YES)
[self.buttonPlayMusic setTitle:@"Stop" forState:UIControlStateNormal];
self.toggleButonPlayMusic = NO;
NSLog(@"RRR");
else
[self.buttonPlayMusic setTitle:@"Play" forState:UIControlStateNormal];
self.toggleButonPlayMusic = YES;
NSLog(@"ZZZ");
【问题讨论】:
应该没问题。已检查以确保按钮不为零(即,由于某种原因,您实际上没有得到对它的引用。) self.toggleButonPlayMusic 这是一个布尔变量吗? @eptdeveloper 是的,它是一个布尔值 【参考方案1】:不需要额外的布尔变量,只需输入此代码即可正常工作
-(void)viewDidLoad
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
self.buttonPlayMusic = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buttonPlayMusic.frame = CGRectMake(((scrollView.frame.size.width - 200) / 2) + cx, 360, 200, 50);
[self.buttonPlayMusic setTitle:@"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonPlayMusic setSelected:TRUE]; //YES to play, NO to stop music
[scrollView addSubview:self.buttonPlayMusic];
-(IBAction)buttonPressed:(id)sender
if (self.buttonPlayMusic.selected == TRUE)
[self.buttonPlayMusic setTitle:@"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(@"RRR");
else
[self.buttonPlayMusic setTitle:@"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(@"ZZZ");
【讨论】:
我不认为布尔值是这里的问题。这可能是不必要的,但不会破坏代码,尤其是当他说 if-else 子句正常工作时。 布尔值不是问题,但是按钮它会自我检查它的状态,所以不需要额外的布尔值来做同样的事情,而你可以用它自己的按钮来检查它。 确实如此,而且它完全是一个更好的实现。【参考方案2】:确保您使用 ctrl drag 将引用从文件拖到 viewController 并将其放入 @interface(有时您可能会不小心摆脱这些)。 This post 有一些方法可以确保正确设置连接。
【讨论】:
【参考方案3】:试试这个
-(IBAction)buttonPressed:(id)sender
UIButton *btn =(UIButton*)sender;
if (self.buttonPlayMusic.selected == TRUE)
[btn setTitle:@"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(@"RRR");
else
[btn setTitle:@"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(@"ZZZ");
【讨论】:
【参考方案4】:-(IBAction)buttonPressed:(id)sender
if (self.buttonPlayMusic.titleLabel.text == @"Play")
[self.buttonPlayMusic setTitle:@"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(@"RRR");
else if (self.buttonPlayMusic.titleLabel.text == @"Stop")
[self.buttonPlayMusic setTitle:@"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(@"ZZZ");
【讨论】:
以上是关于单击时更改 UIButton 文本的主要内容,如果未能解决你的问题,请参考以下文章
Swift:如何在单击提交按钮时更改 UIButton 图像
想要在单击相同的 UIButton 时从 UITableViewCell 更改正确的 UIButton 图像