按钮不显示视图

发布于 2025-01-02 02:16:24 字数 1971 浏览 1 评论 0原文

我已将多个操作添加到一个按钮

  1. 播放音频文件
  2. 显示视图数组

按钮正在播放音频文件但不显示视图。

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

  1. Play audiofile
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女中豪杰 2025-01-09 02:16:24

这里有一些你应该做的小修正——它们可能并不能全部帮助解决问题,但你永远不知道会发生什么。

在这里添加一个完成块:

[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];

我不太记得了,但我认为 setViewControllers 是异步完成的 - 在完成处理程序完成之前无法添加任何内容。要添加该块,您只需放入一个 ^ 和一个代码块(就像普通方法一样):

[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^{
   // your code here.
}];

这可能会解决问题,但在我回到计算机之前我无法确定。

您还应该在每个方法的开头放置一个 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:

[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];

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):

[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^{
   // your code here.
}];

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 the autorelease from the pageViewController - it may be released because it isn't being used.

柠檬色的秋千 2025-01-09 02:16:24

我认为问题出在 displayviewsAction 中,尝试一些更简单的操作,例如:

- (void) displayviewsAction:(id)sender
{
    UIView* testView = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 30.0, 30.0)];
    testView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:testView];
    [testView release];
}

这应该可以帮助您缩小问题范围。根据您的最终目标,您可能不需要所有这些视图控制器;而是考虑 UIView 的 transitionFromView... 用于动画效果。

I think the issue is in displayviewsAction, try something more simple like:

- (void) displayviewsAction:(id)sender
{
    UIView* testView = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 30.0, 30.0)];
    testView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:testView];
    [testView release];
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文