0

My program execute the buttonPressed method and go through the if else statement but the text on my button still does not changed. Is there something I had missed? I even already customized it in xib but the text of the buttonPlayMusic is remained as "Play" even it goes into the other statement.

I think I had connected the button too. 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");
}   
}
shoujo_sm
  • 3,173
  • 4
  • 37
  • 60

4 Answers4

1

no need to take extra boolian variable just put this code and it worked fine

-(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");
    }
}
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
  • I don't think the boolean is the issue here. It may be unnecessary, but it wouldn't break the code, particularly when he said the if-else clauses are working correctly. – AdamG Sep 03 '13 at 04:25
  • boolian is not the issue but button it self check it's state so no need to take extra boolian for doing the same thing while you can check it with button it self. – D-eptdeveloper Sep 03 '13 at 04:27
  • That's true, and it is totally a better implementation. – AdamG Sep 03 '13 at 04:28
0

Make sure that you are dragging a reference from the file to your viewController using ctrl drag and putting it in your @interface (sometimes you can accidentally get rid of these). This post has some methods for making sure the connection is set up properly.

Community
  • 1
  • 1
AdamG
  • 3,718
  • 2
  • 18
  • 28
0

Try this

-(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");
    }
}
Jitendra
  • 5,055
  • 2
  • 22
  • 42
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
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");
    }
}
Ganesh
  • 524
  • 1
  • 4
  • 16