Domanda

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");
}   
}
È stato utile?

Soluzione

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");
    }
}

Altri suggerimenti

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.

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");
    }
}
-(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");
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top