2012-02-03 32 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