警报视图不起作用

时间:2014-05-24 10:11:12

标签: ios iphone uialertview

我有一个按钮,按钮会发送一个对webservice的调用并返回一些值。我想显示一个没有按钮的警告框,直到值从webservice返回。但是在收到值后,alertWait会显示。我尝试使用设置警报功能并从按钮开始调用,但是在接收值结束时也会显示。我在哪里错了是我的代码。

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

   }

3 个答案:

答案 0 :(得分:1)

将服务调用放在后台线程上,就像下面更改的示例代码一样:

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

}

答案 1 :(得分:1)

您需要的是一个活动指示器,它会向用户显示某些活动正在进行。我使用了MBProgressHUD,效果很好。

简要定义它的实现。

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

或使用块

@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");
}

请注意我有NSURLConnection用于演示目的。 希望这可以帮助。

答案 2 :(得分:1)

我认为您的代码存在以下问题:
按下按钮时,"等待"设置并显示警报视图(因为这是在主线程上完成的)。但是之后,如果上传成功,请检查status变量。这通常应该,因为上传需要时间 因此,通常情况下,状态变量不应表示上传成功,并且"等待"警报应留在屏幕上。
在这种情况下,用户只能忽略"等待"提醒,并显示"成功上传"当他/她再次按下按钮时发出警报,这可能是不可能的,因为"等待"显示警报。
您需要做的是使用Web访问中的回调方法来告诉您它已被终止。在这个回调方法中,你不得不忽略"等待"警告,ans显示"成功上传"消息。