EXC_Bad_Request"仅"在调用uialert 3次或更多次之后

时间:2014-03-19 23:27:07

标签: iphone ios7 uialertview exc-bad-access

我是目标c的新手;

我在创建UIAlertview实例化语句时遇到错误(UIAlert * myAlert,它们位于不同的范围内)

我已经引用了以下stackoverflow条目,例如Thisthisthis too以及更多关于互联网的研究。

我无法突破

以下是我的提醒电话

这是我的视图控制器代码,我已经提出了" UIAlertViewDelegate"

#import <UIKit/UIKit.h>

@interface gameFirstViewController : UIViewController<UIAlertViewDelegate>

@end

这是我的班级宣言

#import <Foundation/Foundation.h>

@interface GameLogic : UIView
//all sorts of various non relevant property declarations
@end

以下是我对警报的实施

//action to take when an alert is shown
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger) buttonIndex
{  NSLog(@"OK Tapped");
   NSUserDefaults *MySettingsData = [NSUserDefaults standardUserDefaults];
   row=  [MySettingsData integerForKey:@"Row_count"];
   col = [MySettingsData integerForKey:@"Column_count"];
   if(buttonIndex==0)
   {      
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<col;++j)
        {
            myarr[i][j]=0;
        }
    }
    if(_TimerStatus ==1)
        {
            [mainTimer invalidate];
            mainTimer=nil;
            _TimerStatus=0;
        }
        [self super_reset];
    [self setNeedsDisplay];
    NSLog(@"Game reset");
    return;
    }
}

//我在两个不同的地方使用警报

UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: @"GAME OVER"
                                                         message:@"You clicked on a mine, tap on ok to reset"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil, nil];
        [myAlert performSelectorOnMainThread:@selector(show)
                                  withObject:nil
                               waitUntilDone:YES];

        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle: @"You Won! Whoo Hoo"
                                                         message:@"You have successfully dodged every minefield"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil, nil];
                    [myAlert performSelectorOnMainThread:@selector(show)
                                              withObject:nil
                                           waitUntilDone:YES];

我不确定我哪里出错了,任何帮助都会很棒!

感谢。

1 个答案:

答案 0 :(得分:2)

创建UIAlertView的方式在ARC中并不安全。如果您立即使用该对象,但是您正在传递performSelectorOnMainThread方法,那就没问题了。 ARC可以在主线程执行选择器时释放myAlert变量。这可能是你看到EXC_Bad_Request的原因。

使myAlert成为一个强大的属性,因此它可以存活,或者延迟创建UIAlert,直到主线程能够调用show方法。你可以这样做:

dispatch_async(dispatch_get_main_queue(), ^{
    [[[UIAlertView alloc] initWithTitle:@"GAME OVER" message:@"You clicked on a mine, tap on ok to reset" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
});

注意:我删除了你的双nil标题。我不认为这会有所作为,但以防万一。