I add a function to dismiss the UIAlertView after several seconds.The whole code is like:
- (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];
}
}
the netWorkAlert is invoked when the network is unavailable.
Now the problem I met is when the netWorkAlert is invoked at the second time, the app is broken and the Xcode shows error in
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([ZJAppDelegate class]));
//Thread 1 :EXC_BAD_ACCESS(code=1,address=xc0000004)
}
}
I didn;t use ARC and I don’t know why it crashes. Even I comment the [alert release];, it still has the same problem at the second time.
Could anyone help me to check it?
thanks!
The UIAlertView could be out of scope by the time the
dismissAlertmethod is called (your checking foralertbeingnilwill prevent this code crashing. There is, however, a better way of implementing this wherealertwill never be out of scope.Your class that defines the
networkAlertmethod should implement the<UIAlertViewDelegate>protocol. The code below allows you to intercept the user clicking the ‘cancel’ button and perform a custom action. The default action of pressing cancel is to close theUIAlertView.@interface YourClassName : UIViewController <UIAlertViewDelegate> {}