UIAlertview exc_bad_access [英] UIAlertview exc_bad_access
问题描述
我添加了一个函数以在几秒钟后关闭UIAlertView
.整个代码如下:
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];
}
}
netWorkAlert
在网络不可用时被调用.
the netWorkAlert
is invoked when the network is unavailable.
现在我遇到的问题是第二次调用netWorkAlert
时,应用程序已损坏并且Xcode显示错误
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)
}
}
我没有使用ARC,也不知道为什么它崩溃.即使我评论[alert release];
,第二次它仍然有相同的问题.
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!
推荐答案
在调用dismissAlert
方法时,UIAlertView可能不在范围内(您检查alert
是否为nil
可以防止此代码崩溃但是,还有一种更好的方法可以实现alert
永远不会超出范围.
The UIAlertView could be out of scope by the time the dismissAlert
method is called (your checking for alert
being nil
will prevent this code crashing. There is, however, a better way of implementing this where alert
will never be out of scope.
您定义networkAlert
方法的类应实现<UIAlertViewDelegate>
协议.下面的代码使您可以拦截用户单击取消"按钮并执行自定义操作.按取消的默认操作是关闭UIAlertView
.
Your class that defines the networkAlert
method 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 the UIAlertView
.
@interface YourClassName : UIViewController <UIAlertViewDelegate> {}
@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
}
这篇关于UIAlertview exc_bad_access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!