繁体   English   中英

UIButtons事件触摸内部无法正常工作?

[英]UIButtons event Touch up Inside not work properly?

我的问题很少,这就是为什么我发布了我的所有代码,所以我请求每个人请在给我答案之前测试所有这些代码.Thanx

在此输入图像描述

在我的应用程序中,我以编程方式创建所有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我没有听到任何咔嗒声,当我点击Next UIButtons touchup在事件中发生崩溃时。任何人都可以帮我解决这个问题.Thanx

应用程序由于未捕获的异常NSInternalInconsistencyException ,原因: 无法在bundle中加载NIBNSBundle </Users/moon/Library/Application Support/iPhone Simulator/4.1/Applications/3E14E5D6-4D1B-4DAC-A2B9-07667E99C399/soundtesting.app‌​> (loaded)名称为secondclass

没错。 我们这里有一个无法理解日志消息。 我只能假设您的UIButton将新视图推送到堆栈,它引用的是一个根本不存在的NIB。 这是一个命名问题,而不是按钮问题。 最有可能的是,我会检查对-initWithNibName:任何调用-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];
}

原因可能是内存泄漏,因为播放相同的音频很多次,它可能会填充内存使用此代码播放音频

- (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];
}

我测试你的代码。 代码中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];   
}

你有内存问题。 当你这样做时:

- (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永远不会发布

所以你需要在delegate方法中释放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];
}

我认为你的问题应该解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM