我使用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];
}
而且奇怪的是,應用程序崩潰只有當我彈奏的聲音按鈕點擊方法,否則,如果我在視圖中播放它確實加載然後它的作品沒有任何問題,抓住我的頭兩天,但找不到任何解決方案,請幫我
爲什麼要在那裏創建autorelease池? –
我前兩天還發布了這個問題,所以有一個堆棧溢出提示我要做,早些時候我沒有使用autorelease池,但它沒有任何區別,得到相同的結果 –
爲什麼你聲明兩個'tapSound 'times @property(nonatomic,retain)AVAudioPlayer * tapSound;是足夠的,在這之後確定它應該在你的'viewController.m'類中合成 – TheTiger