添加了多个动作@selector

时间:2012-02-03 22:42:49

标签: iphone

在mainviewcontroller上有一个playpause按钮。这个按钮基本上是在播放和暂停按钮之间切换,工作正常。现在我添加了另一个动作到playpause按钮。当我点击播放按钮时,它不会在播放和暂停之间切换时同时显示网页浏览。

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ModalViewController.h"
#import "PageOneViewController.h"

@interface MainViewController : UIViewController <ModalViewControllerDelegate>

- (void)modalViewAction:(id)sender;

@property (nonatomic, retain) UIToolbar *toolbar;
@property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ModalViewController *viewController;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (strong, nonatomic) UIPageViewController *pageViewController;

@end



@synthesize pageViewController = _pageViewController;


UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:@selector(pageViewControllerAction:) 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];
    }    
}

- (void)pageViewControllerAction:(id)sender
{
    CGRect pageViewRect = self.view.bounds;
    pageViewRect = CGRectInset(pageViewRect, 40.0, 40.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];
}

尝试异常断点,它在

处显示异常断点

[audioPlayer play];

还尝试在

添加断点

-(void)pageViewControllerAction:

它没有执行。

任何想法发生了什么。我做错了什么。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

好吧,它没有执行

(void)pageViewControllerAction:(id)sender 

因为你在上一个方法中有例外吗?这个 audioPlayer 在哪里定义和初始化?这可能就是问题所在。

其次,为什么你需要两个动作。只需要一个并从内部调用另一个函数。

示例:

UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
  //  [playpauseButton addTarget:self action:@selector(pageViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 50, 50);



-(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];
}    


[self pageViewControllerAction]; // Here is the call to the other function
}

修改 最后,确保您在.h文件中定义了函数,如下所示:

- (void) pageViewControllerAction;

除了: 我没有得到你的图像设置逻辑。看起来像那个逻辑中的错误。您还需要在按下按钮后声明 UIControlStateSelected 。您只需设置 UIControlStateNormal

答案 1 :(得分:1)

您需要将发件人添加为选择器的参数:

- (void)pageViewControllerAction:(id)sender
    { ... }