简体   繁体   中英

app crashes when clicks on alert in ios

Following are the code for displaying alert in a view controller

-(void)saveProducts {
    pData = [[JsonModel sharedJsonModel] prodData];
    if ([pData count] == 0 && [self respondsToSelector:@selector(alertView:clickedButtonAtIndex:) ]  ) {
        alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"No products against this category" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }


    [self.tblView reloadData];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        [actInd stopAnimating];
    }

}

But in slow network, alert will come slowly. If we click on back button of navigation bar at the mean time, pop the navigation controller and displaying alert in new view controller. But when i clicks on OK, app suddenly crashes with EXC_BAD_ACCESS error. I also tried

didDismissWithButtonIndex

function instead of

clickedButtonAtIndex

But same error occurs. Please help me

It works normally if we didn't click on back bar button. Problem only arises when first view controllers alert displays in second view controller

EDIT This is the error report * -[ProductsListing alertView:didDismissWithButtonIndex:]: message sent to deallocated instance 0x8478280

EDIT I understand the problem. When I click on back button, my alert delegate deallocates and delegate calling results error. How can I overcome this?

My best guess is that either 'self.navigationController' or 'actInd' have already been released. Also, your 'UIAlertView' leaks memory (unless you're using ARC). Profile the app using Instruments, selecting the "Zombies" tool and see what it comes up with.

From what you described the problem here may be this(a wild guess)

[actInd stopAnimating];

called after the viewController is removed(popped).the actInd may not have a valid memory and hence it crashes

change the method content like this and check

if (buttonIndex == 0) {
        [actInd stopAnimating];
        [self.navigationController popViewControllerAnimated:YES];
    }

Happy coding :)

I believe you have to change

[alert show];

to

if(self.view.window){
   [alert show];
}

This way the alert appears only if the controller(the view) is still on screen.(why let the user see an alert from a previous screen?) If you want the alert to appear anyway....then the "old" controller must inform the "new" one that a problem occurred...and now its the new controller's job to inform the user.

Or you can try changin this part

    [self.navigationController popViewControllerAnimated:YES];
    [actInd stopAnimating];  

to

if(self.view.window){
    [self.navigationController popViewControllerAnimated:YES];
    [actInd stopAnimating]; // im not sure where the animation is...so not sure if this shoulb be in here or not
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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