UIButton被点击两次

时间:2013-10-04 20:04:43

标签: ios objective-c ios7

我需要以编程方式点击按钮:[play sendActionsForControlEvents:UIControlEventTouchUpInside]; 但是当我这样做时,虚空被召唤了两次!我需要启动AVPlayer(在ViewDidLoad中),但音乐也开始了两次。请帮忙:)。

游戏代码:

- (IBAction)playStream:(id)sender  {
    NSLog(@"%@", @"PlayStream clicked: ", sender);
        if(clicked == 0) {
        clicked = 1;
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
        [play setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
        [playerr play];
    }
    else {
        [playerr stop];
        [play setImage:[UIImage imageNamed:@"Start"] forState:UIControlStateNormal];
        [audio setActive:NO error:nil];
        clicked = 0;
    }
} 

1 个答案:

答案 0 :(得分:1)

怎么样:

@implementation MyClassName{
     BOOL musicPlaying;
}

- (IBAction)playStream:(id)sender  {
    if(musicPlaying){
        [self pauseMusic];
    }else{
        [self playMusic];
    }
}

-(void)playMusic{
    musicPlaying= YES;
    [play setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [playerr play];
}

-(void)pauseMusic{
    musicPlaying= NO;
    [playerr stop];
    [play setImage:[UIImage imageNamed:@"Start"] forState:UIControlStateNormal];
    [audio setActive:NO error:nil];
}

现在,不要以编程方式点击按钮,只需调用playMusic或pauseMusic。