Objective c 类中的iOS EXC\u错误\u访问错误
我在我的应用程序中遇到EXC_BAD_访问错误。或者更具体地说在我的一门课上。它是自定义UIAlertView类。当它在使用中抛出EXC\u BAD\u访问时,我无法捕获。有时它就像预期的那样工作得很好,在所有的苏顿,它摇摇欲坠。。。这是全班同学Objective c 类中的iOS EXC\u错误\u访问错误,objective-c,ios,xcode,Objective C,Ios,Xcode,我在我的应用程序中遇到EXC_BAD_访问错误。或者更具体地说在我的一门课上。它是自定义UIAlertView类。当它在使用中抛出EXC\u BAD\u访问时,我无法捕获。有时它就像预期的那样工作得很好,在所有的苏顿,它摇摇欲坠。。。这是全班同学 @implementation AlertPassword int counter = 3; @synthesize done; @synthesize alertText; @synthesize msg; - (void) showAlert :(
@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;
我想可能是因为我没有发布警报。但增加了[警报发布]代码>在用户尝试输入某些内容后直接进行EXC_BAD_访问。无[警报发布]代码>它可以工作。但有时,它会因进出不良而崩溃
有时它也会
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\u BAD\u访问或无法识别的选择器将抛出。EXC\u BAD\u访问主要是由于内存处理错误。警报很可能已变成僵尸。。。我会将此警报作为具有强/保留的属性。您应该在显示时按住它。“秀”后不发布
当你建立第一个你可以这样做
_alert = [[UIAlertView alloc] initWithTitle:@"Security" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; // retain count: 1
注意:调用“show”也会保留它,但这不会改变您也需要这样做的事实
[_alert show]; // retain count: 2
等待委托回调并释放它
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[_alert release], _alert = nil; // retain count in next run will be 0
}
提示:如果将UIAlertView与块结合使用,可能更容易处理 我看不出您在哪里初始化警报,无论如何,我认为它是一个类字段,所以请保留它。如果它不是类实例变量,请将其设置为类实例变量,这样您将始终有一个指向它的指针
[__NSMallocBlock__ dismissWithClickedButtonIndex:animated:]
您正在将此消息发送到原始malloc块,因此可能已释放警报并指向用于其他对象的内存,而这些对象不是objc对象。
尝试保持警惕,看看会发生什么。可能是您将错误的值传递给此“dismissWithClickedButtonIndex:animated:”方法,该方法无法识别值签名,请对此进行双重检查 例外的答案是有道理的,很可能是原因
但这是什么?
[NSNotificationCenter defaultCenter]removeObserver:UITextFieldTextDidChangeNotification] 我应该在哪里发布它?这段代码的想法是,如果用户在显示后输入了错误的密码did(strong,retain)和deleted release,那么在显示后立即显示另一个警报。没有更多的错误。所以我接受你的回答。非常感谢你的帮助。但是是的!问题是你在调用“show”之后就发布了。那么我应该在哪里发布它呢?这是代码。我只在这节课上用过,看一看。我看不出有什么不好的。你能把调用这个方法和整个方法的代码发出去吗?保持警惕,看看它是否崩溃。当然也可能是个定时炸弹:D