多种方法:按钮的目标和选择器不工作

时间:2012-06-15 17:25:56

标签: iphone objective-c

尝试解决我的Other question。这次尝试这种方式 为一个按钮添加了多个方法,但是当单击按钮时,它看起来没有任何效果。

//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];
}

点击它时显示播放按钮没有任何反应。它应播放音频文件,启动NSTimer,开始加载视图并在播放和暂停按钮之间切换。知道什么是错的。

感谢。

1 个答案:

答案 0 :(得分:0)

当用户点按该按钮时,playAction:会拨打[audioPlayer play]。 之后,playpauseAction:会立即评估[audioPlayer isPlaying]YES[audioPlayer pause]