UIAlertView退出EXC_BAD_ACCESS错误
我有这样的错误:当我单击 navigationbar.backItemButton 时,我显示带有两个按钮的 UIAlertView 。当我按下其中任何一个时,应用程序仅以 EXC_BAD_ACCESS 终止。方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 不调用。我该如何解决?谢谢!
//h - 文件
@interface DetailsTableViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>
//m - 文件
- (void)viewWillDisappear:(BOOL)animated
{
//if changes unsaved - alert reask window
if (isDirty)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Save changes?"
message:@"Press YES if you want to save changes before exit, NO - other case."
delegate: self
cancelButtonTitle: @"NO"
otherButtonTitles: @"YES", nil];
[message show];
[message autorelease];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex: buttonIndex];
if([title isEqualToString: @"YES"])
{
[self saveBtnUserClick];
}
}
I have such error: when I click navigationbar.backItemButton I'm showing UIAlertView with two buttons. When I press on any of them application terminates just with EXC_BAD_ACCESS. Method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex don't called. How can I solve it? Thanx!
//h - file
@interface DetailsTableViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>
//m - file
- (void)viewWillDisappear:(BOOL)animated
{
//if changes unsaved - alert reask window
if (isDirty)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Save changes?"
message:@"Press YES if you want to save changes before exit, NO - other case."
delegate: self
cancelButtonTitle: @"NO"
otherButtonTitles: @"YES", nil];
[message show];
[message autorelease];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex: buttonIndex];
if([title isEqualToString: @"YES"])
{
[self saveBtnUserClick];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为问题是,在您点击后退按钮后,您当前的控制器将从导航堆栈中删除并释放,因此当警报尝试调用其委托方法时,它会在释放的对象上调用它们,从而导致
EXC_BAD_ACCESS
错误。为了解决这个问题,我看到了两个明显的选择(尽管可能有更好的解决方案):I think the problem is that after you tapped back button your current controller is removed from navigation stack and deallocated, so when alert tries to call its delegate methods it calls them on deallocated object which results in
EXC_BAD_ACCESS
error. To workaround the problem I see 2 obvious options (although there may be better solutions):尝试将 delegate 更改为 nil 而不是 self。它解决了我的问题。
Try Changing delegate to nil instead of self. It fixed my issue.
您的视图控制器是否实现了 UIAlertViewDelegate?如果没有,请在 { 开始之前添加接口声明。
还可以在 clickedButtonAtIndex 方法中尝试 NSLogging 并打印 buttonIndex 值并查看控制台。
编辑:再次阅读您的帖子,我猜您确实错过了界面声明中的 UIAlertViewDelegate 。
Is your view controller implementing the UIAlertViewDelegate? If not, add in you interface declaration before the { starts.
Also try NSLogging inside the clickedButtonAtIndex method and print the buttonIndex values and see the console.
Edit: Reading your post again, I guess you indeed have missed the UIAlertViewDelegate in your interface declaration.
可能是[消息自动释放];
你用错了吗
【消息发布】;
因为你已经使用了 [[UIAlertView alloc] init.....];你应该释放内存。
autorelease 可以与内存依赖于编译器的结构一起使用,或者您没有手动给出内存。
享受。
Probably [message autorelease];
is you mistake use
[message release];
Because you have used [[UIAlertView alloc] init.....]; there for you should release the memory.
autorelease is something will work with the structure which memory is compiler dependent or you have not given the memory manually.
Enjoy.
“尝试将 delegate 更改为 nil 而不是 self。它解决了我的问题。”为我工作。谢谢
"Try Changing delegate to nil instead of self. It fixed my issue." worked for me. Thanx