如何在我打电话时显示UIAlertView?

时间:2015-10-12 18:53:35

标签: ios objective-c uialertview

所以,类似于这个问题:

UIAlertView taking time to show

我遇到了一个问题,我的UIAlertView在我的所有其他代码运行之后才显示。

我尝试过使用

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

});

但我无法改变发生的事情。目前,用户单击按钮并触发createAccountButtonReleased。然后它似乎跳过dispatch_async部分,运行其余的代码,然后回到它。

- (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:\"username@domain.com\"", @"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];
}

有什么建议吗?

0 个答案:

没有答案