简体   繁体   中英

Multiple methods: Target and Selector of button not working

Trying to solve my Other question . This time trying this way Added multiple methods to one button but when clicking on button it does nothing looks like those methods are not working.

//Add Play Button

UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 30, 30);
[playpauseButton setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];


- (void)playAction:(id)sender{
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
                                              target:self
                                            selector:@selector(displayviewsAction:)
                                            userInfo:nil
                                             repeats:NO];
}  

-(void)playpauseAction:(id)sender {
        if ([audioPlayer isPlaying]){
            [sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
            [audioPlayer pause];
            [self pauseTimer];
    } else {     
        [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
        [audioPlayer play];
        [self resumeTimer];  
        self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
                                        target:self
                                        selector:@selector(displayviewsAction:)
                                        userInfo:nil
                                        repeats:NO];   
        }         
}

 -(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}

It shows play button when clicking on it nothing happens. It should play the audio file, start the NSTimer, start loading views and toggle between play and pause button. Any idea what is wrong.

Thanks.

When the user taps the button, the playAction: calls [audioPlayer play] . Immediately afterwards, the playpauseAction: evaluates [audioPlayer isPlaying] to YES and does [audioPlayer pause] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM