2012-02-06 60 views
0

我有一個UIButton,它具有與之關聯的UIButton顯示視圖,但不能切換到暫停按鈕

  1. 要播放的音頻文件
  2. 播放暫停按鈕之間切換
  3. 顯示視角的多個動作

如果我註釋掉displayViewAction:它工作正常(即播放音頻文件,也切換到暫停按鈕)。但是,如果我使用displayViewAction:方法播放音頻文件顯示視圖,但在顯示視圖之前應立即切換暫停按鈕的狀態,但不是。

代碼波紋管以供參考:

代碼的UIButton:

UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside]; 
[playpauseButton addTarget:self action:@selector(displayviewsAction:) forControlEvents:UIControlEventTouchUpInside]; 
playpauseButton.frame = CGRectMake(0, 0, 50, 50); 
[playpauseButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 
[playpauseButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected]; 

UIBarButtonItem *playpause = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton]; 

代碼Playpause按鈕:

-(void)playpauseAction:(id)sender 
{ 
    if ([audioPlayer isPlaying]){ 
     [sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 
     [audioPlayer pause]; 
    } 
    else { 
     [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal]; 
     [audioPlayer play]; 
    } 
} 

代碼顯示視圖動作:

- (void)displayviewsAction:(id)sender 
{ 
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease]; 
    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease]; 
    [self.view addSubview:viewController.view]; 
} 

燦任何人提醒我如何才能使它以這種方式工作?

回答

2

只附加一個動作到你的UIButton。當這個動作被調用時,根據應用程序的狀態執行各種任務。

1

你不能附加許多行動到你的UIButton。

爲什麼不附加切換播放/暫停功能,然後顯示視圖?

[playpauseButton addTarget:self action:@selector(playpauseActionAndDisplayView:) forControlEvents:UIControlEventTouchUpInside]; 

然後

-(void)playpauseActionAndDisplayView:(id)sender 
{ 
    [self playpauseAction:sender]; 
    [self displayviewsAction:sender]; 
} 
+0

以黃色獲取警告,表示實例方法playpauseAction未找到,並且displayviewsAction未找到 – user1120133 2012-02-06 17:27:21

+0

仍然不能切換以立即暫停按鈕 – user1120133 2012-02-06 17:43:02

+0

當然,您應該在頭文件中聲明您的playpauseAction。 – Martin 2012-02-07 10:22:31

0

改變了UIControlState在playpauseAction方法這樣

-(void)playpauseAction:(id)sender 
{ 

    if 

    ([audioPlayer isPlaying]){ 

[sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateSelected]; 

     [audioPlayer pause]; 

    } else { 

[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal]; 

     [audioPlayer play]; 

     [self displayviewsAction:sender]; 

    } 


} 

和它的工作。