2012-02-03 64 views
0

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

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

它不执行这一点。

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

感谢您的帮助。

回答

1

您需要发送方添加为选择的参数:

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

不,它是在那里,我忘了在这里输入它 – user1120133 2012-02-03 22:59:26

+0

你有没有在你的头文件中定义它们? – 2012-02-03 23:00:07

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

+0

audioplayer定义在viewdidload – user1120133 2012-02-03 23:30:33

+0

当我键入[self pageViewControllerAction];在黄色警告实例方法pageViewControllerAction没有找到 – user1120133 2012-02-03 23:50:12

+0

啊,这告诉你的东西是正确的?发布您的.h文件。您没有在.h文件中添加该功能。请参阅编辑回复 – mbh 2012-02-03 23:58:31