我在我的应用上运行了这个代码,现在在iOS 6上它崩溃了我的应用程序:
// Another method
[NSThread detachNewThreadSelector:@selector(askServerForNFeID)
toTarget:self withObject:nil];
- (void)askServerForNFeID {
if ([response isEqualToString:@"XXXX"]) {
NSString *responseMessage = [NSString stringWithFormat:
NSLocalizedString(@"Autorizado o Uso da NFe\n para chave:\n%@", @""),
[invoiceInfo NFeID]];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Sefaz respondeu:", @"")
message:responseMessage
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
我已经知道从第二个线程调用警报会导致崩溃,因此我更改了代码以从主线程调用警报,如下所示:
if ([response isEqualToString:@"XXXX"]) {
[self performSelectorOnMainThread:@selector(showAlertHelper:)
withObject:[[NSArray alloc] initWithObjects:
NSLocalizedString(@"Não posso seguir em frente", @""),
NSLocalizedString(@"Você usou .....", @""), @"Fechar", @"Comprar", nil]
waitUntilDone:YES];
注意我正在将标题,消息和按钮解析为showAlertHelper的列表......
-(void)showAlertHelper:(NSArray*)theArray{
if ([[theArray objectAtIndex:3] isEqualToString:@""]) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:nil
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:nil
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
[alertView show];
[alertView release];
}
}
我现在的问题是,我有这个处理程序来捕获被点击的按钮,它不再起作用了。它没有被称为:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:NSLocalizedString(@"Comprar", @"")]) {
// Do stuff to buy credits...
} else if ([title isEqualToString:NSLocalizedString(@"Fechar", @"")]) {
NSLog(@"Fechar was selected.");
}
else if ([title isEqualToString:NSLocalizedString(@"Criar conta", @"")]) {
// Do stuff to create an account...
}
}
答案 0 :(得分:2)
您从UIAlertView的创建方法中删除了委托
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:nil
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
应该是
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:self
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
答案 1 :(得分:2)
问题在于,在重新编写的方法中,您将代理设置为nil
而不是self
。因此,不会调用alertView:clickedButtonAtIndex:
委托方法。将代码更改为以下内容:在self
参数中指定delegate:
:
-(void)showAlertHelper:(NSArray*)theArray{
if ([[theArray objectAtIndex:3] isEqualToString:@""]) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:self
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:nil];
[alertView show];
[alertView release];
} else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:self
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
[alertView show];
[alertView release];
}
}