我在我的应用程序上运行了这段代码,现在在 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...
}
}