iOS-如何在代码启动之前显示alertView

时间:2012-07-19 01:48:13

标签: iphone ios ibaction alertview

我有以下代码:

-(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];
 });
}

我想要解除警报,然后我可以调用表格视图。任何sugestions?

3 个答案:

答案 0 :(得分:5)

显示UIAlertView异步完成,因此如果您在方法顶部调用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
        });
    });
}

答案 1 :(得分:0)

当您的提醒被取消时,您需要在“getContacts:”中执行其余代码。在您的UIAlertView上设置一个代理“self”(显示警报的视图控制器),然后在用户单击按钮关闭警报时执行“addressBook”操作。

例如,实现UIAlertViewDelegate方法alertView:clickedButtonAtIndex:并在那里处理你的地址簿(我为你链接了文档)。

答案 2 :(得分: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];
}