按钮不显示视图
我已将多个操作添加到一个按钮
- 播放音频文件
- 显示视图数组
按钮正在播放音频文件但不显示视图。
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];
播放暂停动作编码
-(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];
}
}
DisplayviewsAction 编码
- (void)displayviewsAction:(id)sender
{
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect, 30.0, 30.0);
self.pageViewController.view.frame = pageViewRect;
// Configure the page view controller
UIPageViewController *pageViewController = [[[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]autorelease];
NSArray *viewControllers = [NSArray arrayWithObject:pageViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self.view addSubview:self.pageViewController.view];
}
任何建议我在代码中缺少什么或做错了什么。
感谢您的帮助。
I have added multiple actions to one button
- Play audiofile
- Display array of views
Button is playing audiofile but not displaying views.
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];
Play Pause Action coding
-(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];
}
}
DisplayviewsAction coding
- (void)displayviewsAction:(id)sender
{
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect, 30.0, 30.0);
self.pageViewController.view.frame = pageViewRect;
// Configure the page view controller
UIPageViewController *pageViewController = [[[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]autorelease];
NSArray *viewControllers = [NSArray arrayWithObject:pageViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self.view addSubview:self.pageViewController.view];
}
Any advice what i m missing in the code or doing wrong.
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些你应该做的小修正——它们可能并不能全部帮助解决问题,但你永远不知道会发生什么。
在这里添加一个完成块:
我不太记得了,但我认为 setViewControllers 是异步完成的 - 在完成处理程序完成之前无法添加任何内容。要添加该块,您只需放入一个
^
和一个代码块(就像普通方法一样):这可能会解决问题,但在我回到计算机之前我无法确定。
您还应该在每个方法的开头放置一个
NSLog
,至少在完成调试之前是如此。这样,您始终知道什么被调用,什么不被调用。如果未调用它,请尝试从pageViewController
中删除autorelease
- 它可能会因为未被使用而被释放。Here are some minor corrections you should make - they may not all help with the problem, but you never know.
Add a completion block here:
I don't remember for sure, but I think
setViewControllers
is done asynchronously - nothing cannot be added until after the completion handler is done. To add the block, you simply put a^
and a block of code in (like a normal method):This may fix the problem, but I cannot be sure until I can get back to my computer.
You should also put an
NSLog
at the beginning of every method, at least until you are done debugging. That way, you always know what is being called and what isn't. If it is not called, try removing theautorelease
from thepageViewController
- it may be released because it isn't being used.我认为问题出在 displayviewsAction 中,尝试一些更简单的操作,例如:
这应该可以帮助您缩小问题范围。根据您的最终目标,您可能不需要所有这些视图控制器;而是考虑 UIView 的 transitionFromView... 用于动画效果。
I think the issue is in displayviewsAction, try something more simple like:
That should help you narrow down the problem. Depending on what your final aim is you might not need all those view controllers; instead consider UIView's transitionFromView... for the animation effects.