问题描述:

So, similar to this question:

UIAlertView taking time to show

I'm running into a problem where my UIAlertView is not displaying until after all of my other code has run.

I have tried using the

dispatch_async(dispatch_get_main_queue(),^{

[self presentLoadingAlertWithTitle:NSLocalizedString(@"Creating Account...", nil)];

});

But I was unable to get it to change what happens. Currently, a user clicks a button and it triggers createAccountButtonReleased. Then it seems to skip over the dispatch_async part, run the rest of the code, and then come back to it.

- (IBAction)createAccountButtonReleased:(id)sender {

dispatch_async(dispatch_get_main_queue(),^{

[self presentLoadingAlertWithTitle:NSLocalizedString(@"Creating Account...", nil)];

});

if ((![self.firstNameTextField hasText] && ![self.lastNameTextField hasText]) || ![self.emailTextField hasText] || ![self.passwordTextField hasText]) {

[self dismissAlert:^(void){ [self presentOkAlertWithTitle:NSLocalizedString(@"An error has occured.", nil) andMessage:NSLocalizedString(@"All text fields should be filled out.", nil)];}];

}

else if (([self.firstNameTextField hasText] || [self.lastNameTextField hasText]) && [self.emailTextField hasText] && [self.passwordTextField hasText]) {

UserCreateWebService *ucws = [[UserCreateWebService alloc]initWithFirstName:self.firstNameTextField.text andLastName:self.lastNameTextField.text andEmail:self.emailTextField.text andPassword:self.passwordTextField.text];

NSString *ucwsResponse = [ucws userCreated];

if ([ucwsResponse isEqualToString:@"Contact already exists"]) {

[self dismissAlert:^(void){[self presentOkAlertWithTitle:NSLocalizedString(@"An error has occured.", nil) andMessage:NSLocalizedString(@"A contact with that information already exists. Please enter a different email and try again.", nil)];}];

}

else if (ucwsResponse.length > 75 && [[ucwsResponse substringToIndex:75] isEqualToString:@"EX: The specified string is not in the form required for an e-mail address."]) {

[self dismissAlert:^(void){ [self presentOkAlertWithTitle:NSLocalizedString(@"An error has occured.", nil) andMessage:NSLocalizedString(@"The entered email is not in the required format. Please enter as:\"[email protected]\"", @"Replace username with the equivalent word and domain with the equivalent word")];}];

}

else if ([ucwsResponse isEqualToString:@"True"]) {

//logs in the newly created user

User *user = [[[UserLoginService alloc]initWithUsername:self.emailTextField.text andPassword:self.passwordTextField.text] attemptLogin];

if (!user) {

[self dismissAlert:^(void){[self presentOkAlertWithTitle:NSLocalizedString(@"Something went wrong.", nil) andMessage:NSLocalizedString(@"Please ensure that you have an internet connection.", nil)];}];

}

else {

//post a notification

}

[self dismissAlert];

UIAlertView *success = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Registration Complete", nil) message:NSLocalizedString(@"Welcome! Press the Ok button to get started!" , nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil]; // Makes Alert View

[success show]; //shows alert view

}

else { //need to beef up the error message with the return from the json.

[self presentOkAlertWithTitle:NSLocalizedString(@"An error has occured.", nil) andMessage:NSLocalizedString(@"Your account has not been created. Please check your internet connection or try again later.", nil)];

}

}

presentLoadingAlertWithTitle:

 - (void)presentLoadingAlertWithTitle:(NSString *)title completion:(void (^)(void))completion

{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:@"\n" preferredStyle:UIAlertControllerStyleAlert];

CGRect activityIndicatorFrame = alertController.view.bounds;

activityIndicatorFrame.size.height -= 35.0f;

activityIndicatorFrame.origin.y += 35.0f;

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityIndicatorFrame];

[activityIndicator setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];

[activityIndicator setUserInteractionEnabled:NO];

[activityIndicator startAnimating];

[alertController.view addSubview:activityIndicator];

[self presentViewController:alertController animated:YES completion:completion];

}

- (void)presentLoadingAlertWithTitle:(NSString *)title

{

[self presentLoadingAlertWithTitle:title completion:nil];

}

Any advice?

相关阅读:
Top