禁用警报视图连接到Internet时

时间:2012-11-22 21:38:06

标签: iphone ios xcode ipad alert

我有一个在没有互联网连接时弹出的警报视图。我有办法在有互联网连接时禁用它.... 工作代码:

-(void)reachabilityChanged:(NSNotification*)note

    {
        Reachability * reach = [note object];

        if([reach isReachable])
        {
            notificationLabel.text = @"Notification Says Reachable";
            NSLog(@"Internet is Up");



        }
        else
        {
            notificationLabel.text = @"Notification Says Unreachable";
            NSLog(@"Internet is Down");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please connect to Internet"
                                                            message:nil
                                                           delegate:self
                                                  cancelButtonTitle:nil
                                                  otherButtonTitle:nil
            [alert show];
        }
    }

-(void)dismissAlert:(UIAlertView *)alertView{
          [alertView dismissWithClickedButtonIndex:0 animated:YES];

}

3 个答案:

答案 0 :(得分:1)

您可以将alertView保留为实例变量,然后在iVar上调用didDismissWithButtonIndex。因此,您可以在viewDidLoad中分配警报并在以下情况后使用它:

-(void)reachabilityChanged:(NSNotification*)note{
        Reachability * reach = [note object];
        if([reach isReachable])
        {
            notificationLabel.text = @"Notification Says Reachable";
            NSLog(@"Internet is Up");
            [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:0];
        }
        else
        {
            notificationLabel.text = @"Notification Says Unreachable";
            NSLog(@"Internet is Down");
            //or you can realloc here your alert
            UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
            progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
            [alert addSubview:progress];
            [progress startAnimating];
            [alert show];
        }
    }

    -(void)dismissAlert:(UIAlertView *)alertView{
              [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }

并确保在头文件中实现UIAlertViewDelegate

答案 1 :(得分:0)

  
      
  • (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  •   

使用buttonIndex 0解析它。

答案 2 :(得分:0)

您可以使用以下实例方法解除UIAlertView

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

[alert dismissWithClickedButtonIndex:0 animated:YES];

当然,您首先需要找到对该警报视图的引用。

UIAlertView class reference