单击时更改UIButton文本

时间:2013-09-03 04:03:05

标签: iphone ios uibutton uicontrol uicontrolevents

我的程序执行buttonPressed方法并通过if else语句,但我的按钮上的文本仍然没有改变。有没有我错过的东西?我甚至已经在xib中对它进行了自定义,但是buttonPlayMusic的文本仍保留为“Play”,即使它进入另一个声明。

我想我也连接了按钮。 XIB

[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 个答案:

答案 0 :(得分: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");
    }
}

答案 1 :(得分:0)

确保使用ctrl拖动将引用从文件拖动到viewController并将其放入@interface(有时您可能会意外地摆脱这些)。 This post有一些方法可以确保正确设置连接。

答案 2 :(得分: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");
    }
}

答案 3 :(得分: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");
    }
}