0

我的程序执行 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");
}   
}
4

4 回答 4

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");
    }
}
于 2013-09-03T04:24:38.093 回答
0
-(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");
    }
}
于 2013-09-03T09:11:51.013 回答
0

尝试这个

-(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");
    }
}
于 2013-09-03T04:43:59.907 回答
0

确保您使用 ctrl drag 将引用从文件拖到 viewController 并将其放入您的 @interface(有时您可能会不小心摆脱这些)。 这篇文章有一些方法可以确保正确设置连接。

于 2013-09-03T04:24:35.077 回答