2012-09-26 42 views
0

我使用uibuttons創建網格視圖,現在需要選擇並取消選擇按鈕的單擊。 所有工作正常,但是當我嘗試播放按鈕上單擊聲音,應用程序獲得通過控制檯打印如下消息崩潰:應用程序在按鈕單擊方法播放聲音時崩潰

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton prepareToPlay]: unrecognized selector sent to instance 0x7526a20'

要播放的聲音,我已經採取AVAudioPlayer的對象克拉斯在.h文件中,並對其進行初始化,鑑於沒有負荷的方法和它玩上按一下按鈕,這裏是我的代碼 .h文件中

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVAudioPlayer.h> 
#import <AudioUnit/AudioUnit.h> 

@interface PlayViewController : UIViewController<AVAudioPlayerDelegate> 
{ 
    IBOutlet UIButton *resetBtn; 
    UIButton *btn[25]; 
    AVAudioPlayer *tapSound; 

} 
-(IBAction)reserBtnClkd; 
@property(nonatomic,retain) AVAudioPlayer *tapSound; 

@end 
in .m file 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]]; 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSString *path=[[NSBundle mainBundle] pathForResource:@"button-3" ofType:@"wav"]; 
    AVAudioPlayer *Tapsound=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    self.tapSound=Tapsound; 
    [Tapsound release]; 
    [pool release]; 
    [self setTheIcons]; 
    resetBtn.layer.cornerRadius=6.0; 
    resetBtn.backgroundColor=[UIColor colorWithRed:90/255.00 green:33.00/255.00 blue:179.00/255.00 alpha:1]; 
    resetBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:22]; 
    [resetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
} 

#pragma mark 
#pragma mark<Creating The Grid View Of Icons> 
#pragma mark 

-(void) setTheIcons 
{ 
    int x=11; 
    int y=55; 

    for(int i=1;i<=25;i++) 
    { 
     btn[i]=[UIButton buttonWithType:UIButtonTypeCustom]; 
     btn[i].frame=CGRectMake(x, y, 58,58); 
     [btn[i] addTarget:self action:@selector(btnClkd:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.view addSubview:btn[i]]; 

     if(i%5==0) 
     { 
      x=11; 
      y=y+60; 
     } 
     else 
     { 
      x=x+60; 
     } 
    } 
    [self reserBtnClkd]; 
} 

-(void)btnClkd:(UIButton*)sender 
{ 
    sender.selected=!sender.selected; 

    if(sender.selected) 
    { 
     // UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(12, 12, 34, 34)]; 
     UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 58, 58)]; 
     tempView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"hover-effectNew.png"]]; 
     tempView.userInteractionEnabled=NO; 
     [sender addSubview:tempView]; 
     [tempView release]; 
    } 
    else 
    { 
     NSArray *subviewToRemove=[sender subviews]; 
     int i=1; 
     for(UIView *view in subviewToRemove) 
     { 
      if(i==2) 
      { 
      [view removeFromSuperview]; 
      } 
      i++; 
     } 
    } 
    [self.tapSound prepareToPlay]; 
    [self.tapSound setDelegate:self]; 
    [self.tapSound play]; 

} 

而且奇怪的是,應用程序崩潰只有當我彈奏的聲音按鈕點擊方法,否則,如果我在視圖中播放它確實加載然後它的作品沒有任何問題,抓住我的頭兩天,但找不到任何解決方案,請幫我

+0

爲什麼要在那裏創建autorelease池? –

+0

我前兩天還發布了這個問題,所以有一個堆棧溢出提示我要做,早些時候我沒有使用autorelease池,但它沒有任何區別,得到相同的結果 –

+0

爲什麼你聲明兩個'tapSound 'times @property(nonatomic,retain)AVAudioPlayer * tapSound;是足夠的,在這之後確定它應該在你的'viewController.m'類中合成 – TheTiger

回答

0

我認爲問題是,你的self.tapSound是一個UIButton(你可以在崩潰日誌中看到它:'-[UIButton prepareToPlay]: unrecognized selector sent to instance 0x7526a20'

嘗試調試它一步一步來,看看會發生什麼

+0

不,不能理解爲什麼會收到此消息? –

0

我想是因爲self.tapSound被引用Tapsound,當你調用[Tapsound release]它釋放特性爲好。我會建議直接實例化該屬性,如下所示:

/*note that you have declared tapSound in your .h 
as a class variable in addition to being a property. 
This means you can access it as both. 
Wherever you would type self.tapSound, instead use simply tapSound 
*/ 

tapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
+0

不,它也行不通,並給出相同的問題 –

+0

嘗試做一些NSLog的。我假設你已經驗證了拋出異常的行是[self.tapSound play]?另外,也許嘗試刪除setDelegate:行? – WolfLink

+0

是的,完成,它正常執行,當控制進入行[self.tapSound播放],它會崩潰 –

相關問題