2

我有以下代码:

-(IBAction)showAlertView:(id)sender{

alertView = [[UIAlertView alloc] initWithTitle:@"Atualizando" message:@"\n"delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show]; 
}


-(IBAction)getContacts:(id)sender {

[self showAlertView:(id)self];

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

我想在 IBAction 的其余部分开始之前显示警报,但我只在 IBAction 结束时看到 alertView。我究竟做错了什么?

编辑:我有:

-(IBAction)getContacts:(id)sender {

// display the alert view
[self showAlertView:self];

// do the synchronous operation on a different queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

....

if ([contact length] == 8) {

            NSString *first = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            NSString *last = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
            NSString *phone = contact;
            ContactInfo *user1 = [[ContactInfo alloc] init];
            user1.first = first;
            user1.last = last;
            user1.phone = phone;
            user1.person = person;
            user1.phoneIdx = j;
            user1.book = addressBook;
            NSLog(@"phone is %@", phone);
            [secondViewController.users addObject:user1];
        }
        ABRecordSetValue(person, kABPersonPhoneProperty, mutablePhones, &error);
    }
}
bool didSave = ABAddressBookSave(addressBook, &error);
if(!didSave){
    NSLog(@"error!");
}
dispatch_async(dispatch_get_main_queue(), ^{
    [self hideAlertView]; // or however you want to do it
});

UIAlertView *alertAlmost = [[UIAlertView alloc] initWithTitle:@"Quase Pronto" message:@"Os seguintes contatos não tem código de área. Porfavor, selecione os contatos que você deseja adicionar o digito 9 e pressione Ok (caso não queira adicionar em nenhum, pressione Ok) " delegate:self cancelButtonTitle:@"Ok!" otherButtonTitles:nil];
[alertAlmost show];

[self presentViewController: secondViewController animated:YES completion: NULL];
 });
}

我希望解除警报,然后我可以调用表格视图。有什么建议吗?

4

3 回答 3

5

显示 aUIAlertView是异步完成的,因此如果您showAlertView:在方法的顶部调用,它将显示警报视图,然后立即返回,然后执行方法的其余部分。

如果您希望在关闭警报视图后发生其余方法,则需要将自己添加为警报视图delegate,然后实现该方法

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

并在那里做剩下的事情。


编辑:好的,我想我明白了你的问题。您正在主队列上执行一些耗时的同步操作,这会阻止它,以便稍后才会显示警报视图。

您应该将耗时的操作移至不同的队列,如下所示:

-(IBAction)getContacts:(id)sender {
    // display the alert view
    [self showAlertView:self];

    // do the synchronous operation on a different queue
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ABAddressBookRef addressBook = ABAddressBookCreate( );
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
        CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

        // once this operation has finished, you can hide the alert view like so:
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hideAlertView]; // or however you want to do it
        });
    });
}
于 2012-07-19T02:02:48.000 回答
0

getContacts:当您的警报被解除时,您需要执行“”中的其余代码。在“”的 UIAlertView 上设置一个委托self(显示警报的视图控制器),然后在用户单击按钮以解除警报时执行“地址簿”操作。

例如,实现 UIAlertViewDelegate 方法alertView:clickedButtonAtIndex:并在其中处理您的地址簿内容(我为您链接了文档)。

于 2012-07-19T02:01:49.897 回答
0

我想你是说你想弹出一个警报,就像一个进度指示器,那个警报出现时,你想开始你的另一个过程。目前,您请求立即显示警报,但正如其他答案所说,该调用是异步的,并且 UI 线程无法在开始其他工作之前显示警报。

你可以试试这个:

-(IBAction)getContacts:(id)sender {

    [self showAlertView:(id)self];
    [self performSelectorInBackground: @selector(initAddressBook) withObject: nil];
}

-(void)initAddressBook {
    ABAddressBookRef addressBook = ABAddressBookCreate( );
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
}

如果在后台运行地址簿工作时出现问题,您甚至可以给 UI 线程足够的时间来发布警报,如下所示:

-(IBAction)getContacts:(id)sender {

    [self showAlertView:(id)self];
    [self performSelector: @selector(initAddressBook) withObject: nil afterDelay: 0.1f];
}
于 2012-07-19T02:20:33.383 回答