我的问题很少,这就是为什么我发布了我的所有代码,所以我请求每个人在给我答案之前测试所有这些代码。感谢
在我的应用程序中,我以编程方式创建所有UIButtons
,然后将所有这些UIButtons
保存在NSMutableArray
中。这是我的代码: -
-(void)button:(id)sender
{
int btnn = 0;
int spacex = 152;
int spacey=20;
int k=0;
saveBtn = [[NSMutableArray alloc] init];
for (int i=0; i<48; i++)
{
if (btnn>6)
{
spacey=spacey+25;
spacex = 152;
btnn = 0;
}
else
{
btnn++ ;
k++;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(spacex, spacey, 25.0, 25.0);
int idx;
idx = arc4random()%[arr count];
NSString* titre1 = [arr objectAtIndex:idx];
[btn setTitle: titre1 forState: UIControlStateNormal];
[btn setTitleColor: [UIColor yellowColor] forState: UIControlStateNormal];
[btn.titleLabel setFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:22.0]];
spacex = spacex + 25;
btn.tag=k;
[btn addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
[saveBtn addObject:btn];
[self.view addSubview:btn];
}
}
}
现在在(aMethod :)我在每个UIButton
TouchUpInside
事件中添加点击声音。这是我的代码。
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
在我的应用程序启动时,每件事情都运行良好,但在100次点击后不断点击UIButtons
我听不到点击声,当我点击事件内的下一个UIButtons
触摸它崩溃时。可以任何人都帮我解决这个问题。谢谢
答案 0 :(得分:2)
应用由于未捕获的异常
NSInternalInconsistencyException
,原因:无法在包中加载NIB :NSBundle </Users/moon/Library/Application Support/iPhone Simulator/4.1/Applications/3E14E5D6-4D1B-4DAC-A2B9-07667E99C399/soundtesting.app> (loaded)
名称为secondclass
啊,是的。我们这里有一个无法理解日志消息。你只能假设将新视图推送到堆栈的UIButton
引用了一个根本不存在的NIB。这是一个命名问题,而不是按钮问题。最有可能的是,我会检查对-initWithNibName:
的任何调用,看看你的NIB是否真的被命名为“secondclass”。请记住,iOS文件名是CaSe SeNsITive。
//.h
...
@property (nonatomic, retain) AVAudioPlayer * player;
//.m
-(void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
player.delegate=self;
//in MRC, use [path release];
}
-(void)someMethod:(id)sender
{
[player play];
}
答案 1 :(得分:1)
原因可能是内存泄漏因为播放相同的音频很多次它可能会填充内存使用此代码播放音频
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundleOrNil
{
if (self = [super initWithNibName:nibName bundle:nibBundleOrNil]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
theAudio = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:NULL];
}
return self;
}
- (IBAction)playSound
{
[theAudio play];
}
- (void)dealloc
{
[theAudio release];
[super dealloc];
}
答案 2 :(得分:1)
我测试你的代码。代码中aMethod的变化。虽然我可以获得标签价值。
- (void)aMethod:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSLog(@"\n\n\nselectedbutton tag === %d \n\n\n",btn.tag );
NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
答案 3 :(得分:1)
你有内存问题。当你这样做时:
- (void)aMethod:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"]; AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate=self; [theAudio play]; }
1 /路径永不释放
2 / theAudio永远不会释放
所以你需要在委托方法中释放音频:
– audioPlayerDidFinishPlaying:successfully:
你需要释放这样的路径:
- (void)aMethod:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"]; AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate=self; [theAudio play]; [path release]; }
我认为你的问题应该解决。