Alert View Not functioning

时光怂恿深爱的人放手 提交于 2019-12-25 02:58:42

问题


I have a button and the button sends a call to webservice and returns some values. I want to show an alert box without buttons until the value returns from webservice. But the alertWait is displaying after receiving the values. i tried using setting up a function for alert and calling in from the start of the button but that too displays at the end of receiving values. Where am i going wrong here is my code.

    - (IBAction)UploadButton:(id)sender
    {
      // Initializtion of some elements

        UIAlertView *alertWait;

        alertWait = [[UIAlertView alloc] initWithTitle:@"Please Wait..."
                                            message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

        [alertWait show];


       //Web service call statements


        if ([status intValue] == 0) 
      {
            NSLog(@"It is zero");
            [alertWait dismissWithClickedButtonIndex:0 animated:YES];
            UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"Success" message:@"You have successfully uploded the document and waiting for approval" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }

   }

回答1:


Put the service calls on a background thread, just like example code changed below :

- (IBAction)UploadButton:(id)sender
{
          // Initializtion of some elements
            UIAlertView *alertWait;

            alertWait = [[UIAlertView alloc] initWithTitle:@"Please Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

            [alertWait show];

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

            //Web service call statements

           dispatch_async(dispatch_get_main_queue(), ^{
                //Do your UI operations here
                if ([status intValue] == 0) 
                {       
                    NSLog(@"It is zero");
                    [alertWait dismissWithClickedButtonIndex:0 animated:YES];
                    UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"Success" message:@"You have 
successfully uploded the document and waiting for approval" delegate:self 
cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                    [alert show];
                }
            });
       });

}



回答2:


What you need is an activity indicator which would show the user that some activity is going on . I have used MBProgressHUD and it works well.

To briefly define the implementation of it .

-(void)login
{
      MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:hud];
        [hud show:YES];
        [hud setLabelText:@"Loading..."];

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        //Network activity
        dispatch_async(dispatch_get_main_queue(), ^{
            //Your alert success alert will go here
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    });
}

Or using blocks

@property (strong,nonatomic)void(^netWorkResultBlock)(NSInteger status);
-(void)login 
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    [hud show:YES];
    [hud setLabelText:@"Loading..."];

    [self sendNetWorkRequesWithResultBlock:^(NSInteger status){
        if (status == 0) {
            NSLog(@"It is zero");

            UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"Success" message:@"You have successfully uploded the document and waiting for approval" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];

            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
    }];
}

-(void)sendNetWorkRequesWithResultBlock:(void (^)(NSInteger status))resultBlock
{
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (resultBlock) {
        [self setNetWorkResultBlock:resultBlock];
    }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.netWorkResultBlock("Your status");
}

Please note i have NSURLConnection for demonstration purposes. Hope this helps.




回答3:


I think the problem with your code is the following:
When the button is pressed, the "wait" alert view is set up and shown (since this is done on the main thread). But immediately afterwards, you check the status variable if the upload has been successfully done. This should normally not be the case since the upload takes time.
So, normally, the status variable should not indicate a successful upload, and the "wait" alert should stay on the screen.
In this case, the user could only dismiss the "wait" alert, and show the "successfully uploaded" alert when he/she presses the button again, which is probably not possible because the "wait" alert is shown.
What you had to do is to use a callback method from the web access that tells you that it has been terminated. In this callback method, you had to dismiss the "wait" alert, ans to show the "successfully uploaded" message.



来源:https://stackoverflow.com/questions/23843752/alert-view-not-functioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!