Dan*_*man 2 iphone ibaction ios 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 );
Run Code Online (Sandbox Code Playgroud)
我希望在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 );
Run Code Online (Sandbox Code Playgroud)
....
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];
});
}
Run Code Online (Sandbox Code Playgroud)
我想要解除警报,然后我可以调用表视图.任何sugestions?
显示a UIAlertView是异步完成的,因此如果您showAlertView:在方法的顶部调用,它将显示警报视图,然后立即返回,然后执行其余的方法.
如果您希望在解除警报视图后发生其余方法,则需要将自己添加为警报视图delegate,然后实现方法
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Run Code Online (Sandbox Code Playgroud)
并在那里做剩下的东西.
编辑:好的,我想我得到了你的问题.你在主队列上花了一些时间进行同步操作并阻塞它,这样才能在以后显示警报视图.
您应该将耗时的操作移动到不同的队列,如下所示:
-(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
});
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |