我在我的应用程序中遇到了 EXC_BAD_ACCESS 错误。或者在我的一门课上更具体一些。它是自定义 UIAlertView 类。当它在使用中抛出 EXC_BAD_ACCESS 时我无法捕捉到。有时它的效果正如预期的那样好,但突然间它就崩溃了……这是全类同学
@implementation AlertPassword
int counter = 3;
@synthesize done;
@synthesize alertText;
@synthesize msg;
- (void) showAlert :(NSString*) title
{
if(counter != 3){
if(counter == 1)
{
NSString *msgs = @"Last warning";
msg = msgs;
}
else
{
NSString *msgs = [NSString stringWithFormat:@"WRONG PIN. %d times remaining",counter];
msg = msgs;
}
}
else
{
NSString *msgs = @"Enter your pin";
msg = msgs;
}
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Security" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
_alert = alert;
_alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
alertText = [_alert textFieldAtIndex:0];
alertText.keyboardType = UIKeyboardTypeNumberPad;
alertText.placeholder = @"Pin";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidChange:)
name:UITextFieldTextDidChangeNotification object:alertText];
[_alert show];
[_alert release];
[[NSNotificationCenter defaultCenter] removeObserver:UITextFieldTextDidChangeNotification];
}
- (void)controlTextDidChange:(NSNotification *)notification {
{
NSString *pin = [[NSUserDefaults standardUserDefaults] stringForKey:@"Pin"];
if ([notification object] == alertText)
{
if (alertText.text.length == pin.length)
{
if(counter != 0)
{
if([alertText.text isEqualToString:pin])
{
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[self.tableViewController openSettings];
counter = 3;
}
else
{
counter--;
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[self showAlert:@""];
}
}
else
{
[_alert dismissWithClickedButtonIndex:0 animated:NO];
[[NSUserDefaults standardUserDefaults] setObject:NULL
forKey:@"Telephone"];
[[NSUserDefaults standardUserDefaults] setObject:NULL
forKey:@"Pin"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"" message:AMLocalizedString(@"EraseData", nil) delegate:nil cancelButtonTitle:AMLocalizedString(@"Ok", nil) otherButtonTitles:nil];
counter = 3;
[av show];
[av release];
}
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *pincheck = [[NSUserDefaults standardUserDefaults] stringForKey:@"pinCheck"];
if (buttonIndex == 0)
{
if(pincheck.intValue == 1)
{
NSLog(@"app kill");
exit(0);
}
else
{
NSLog(@"dismiss");
}
}
}
@end
这是我初始化和使用此类的地方。
case 5:
NSLog(@"Add remove");
if (pincheck != NULL && pin != NULL){
if([pincheck isEqualToString:@"0"])
{
AlertPassword *alert = [AlertPassword alloc];
alert.tableViewController = self;
NSString *msg = @"Enter your pin code to access:";
[alert showAlert:msg];
// [alert release];
}
break;
}
else
{
NSLog(@"Is null");
[Menu load2View:self];
}
break;
我想也许是因为我没有发布警报。但是添加 [alert release];
使得在用户尝试输入内容后立即具有 EXC_BAD_ACCESS 。没有[alert release];
它就可以工作。但有时它会因 EXC_BAD_ACCESS 而崩溃
有时也会这样
2012-11-08 12:11:27.451 kodinisRaktas[2485:19d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ dismissWithClickedButtonIndex:animated:]: unrecognized selector sent to instance 0x947aae0'
但我也不知道为什么会发生这种情况
请帮忙,我对 Objective-C 和 ios 还很陌生,我不知道如何摆脱这个问题,我想有一点经验的人会发现我的代码有什么问题。
我刚刚看到,如果您按取消键 4-5 次或更多次,然后尝试输入某些内容,则会抛出 EXC_BAD_ACCESS 或无法识别的选择器。
最佳答案
EXC_BAD_ACCESS 主要是由于内存处理不当造成的。该警报很可能已成为僵尸...我会将警报作为具有强/保留的属性。展示时您应该握住它。 “表演”后不发布。
当你设置第一个时,你可以这样做
_alert = [[UIAlertView alloc] initWithTitle:@"Security" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; // retain count: 1
请注意,调用“show”也会保留它,但这不会改变您也需要保留它的事实。
[_alert show]; // retain count: 2
等待委托(delegate)回调并释放它。
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[_alert release], _alert = nil; // retain count in next run will be 0
}
提示:如果使用 UIAlertView 结合 block 可能会更容易处理。 http://gkoreman.com/blog/2011/02/15/uialertview-with-blocks/
关于objective-c - iOS EXC_BAD_ACCESS 类中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286857/