我有一个UIButton,它有多个与之相关的动作
如果我注释掉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];
}
任何人都可以告诉我如何让它以这种方式运作?
答案 0 :(得分:2)
只为您的UIButton附加一个操作。调用此操作时,根据应用程序的状态执行各种任务。
答案 1 :(得分:1)
您无法在UIButton上添加许多操作。
为什么不附加切换播放/暂停的功能,然后显示视图?
[playpauseButton addTarget:self action:@selector(playpauseActionAndDisplayView:) forControlEvents:UIControlEventTouchUpInside];
然后
-(void)playpauseActionAndDisplayView:(id)sender
{
[self playpauseAction:sender];
[self displayviewsAction:sender];
}
答案 2 :(得分:0)
更改了playpauseAction方法中的UIControlState
,如下所示
-(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];
}
}
并且有效。