Ios UIAlertview exc\u错误访问

Ios UIAlertview exc\u错误访问,ios,exc-bad-access,uialertview,Ios,Exc Bad Access,Uialertview,我添加了一个函数,可以在几秒钟后关闭UIAlertView。整个代码如下: - (void)netWorkAlert { UIAlertView *netWork = [[UIAlertView alloc] initWithTitle:@"error" message:@"network has problems" delegate:self cancelButtonTitle:nil otherButtonTitles: nil]; [netWork show];

我添加了一个函数,可以在几秒钟后关闭
UIAlertView
。整个代码如下:


- (void)netWorkAlert
{
    UIAlertView *netWork = [[UIAlertView alloc] initWithTitle:@"error" message:@"network has problems" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [netWork show];
    [self performSelector:@selector(dismissAlert:) withObject:netWork afterDelay:2];
}
- (void)dismissAlert:(UIAlertView *)alert
{
    if(alert)
    {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [alert release];
    }
}

当网络不可用时,将调用
netWorkAlert

现在我遇到的问题是,当第二次调用
netWorkAlert
时,应用程序被破坏,Xcode显示错误


int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([ZJAppDelegate class]));
//Thread 1 :EXC_BAD_ACCESS(code=1,address=xc0000004)
    }
}
我没有;我不知道为什么它会崩溃。甚至我也评论了
[警报发布],第二次仍然存在相同的问题

谁能帮我检查一下吗?
谢谢

调用
dismissAlert
方法时,UIAlertView可能已超出范围(您检查
alert
是否为
nil
将防止此代码崩溃。但是,有一种更好的实现方法,
alert
永远不会超出范围

定义
networkAlert
方法的类应实现
协议。下面的代码允许您拦截用户单击“取消”按钮并执行自定义操作。按取消的默认操作是关闭
UIAlertView

@interface YourClassName:UIViewController{}

@implementation YourClassName

-(void) networkAlert
{
    UIAlertView *netWork = [[UIAlertView alloc] initWithTitle:@"error" 
                                                      message:@"network has problems"
                                                     delegate:self 
                                            cancelButtonTitle:@"cancel" 
                                            otherButtonTitles:nil];
    [netWork show];
}

- (void) alertViewCancel:(UIAlertView*)alertView 
{
    what ever it is you want to do when the cancel button is pressed here
}

EXC_BAD_访问是由于访问已发布的对象而导致的。要避免这种情况,请将对UIAlertView的调用设置为某种模式:

职能机构:

-(void)checkSaving
{
    UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"Do you want to add these results to your database?"
        message:@"\n\n"
        delegate:self
        cancelButtonTitle:@"No"
        otherButtonTitles:@"Save", nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
    //this prevent the ARC to clean up :
    NSRunLoop *rl = [NSRunLoop currentRunLoop];
    NSDate *d;
    d= (NSDate*)[d init];
    while ([alert isVisible]) {
        [rl runUntilDate:d];
    }

}
您的选择结果:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 1)//Save
    {
        //do something

    }
    if (buttonIndex == 0)//NO
    {
        //do something
    }
}
在接口声明中注册函数:

@interface yourViewController ()
    -(void)checkSaving
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
@end
致电: [自我检查保存]


我希望这能对您有所帮助。

编辑:为什么不让用户解除警报?让警报消失有点奇怪。当网络不可用时,我想让用户知道。哦,很抱歉,程序由于其他原因而崩溃。我该怎么办?您可以关闭此问题。。