-2

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

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

}

4

3 回答 3

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在您的头文件中实现。

于 2012-11-22T22:06:21.433 回答
0
  • (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

使用 buttonIndex 0 将其关闭。

于 2012-11-22T21:44:48.090 回答
0

UIAlertView您可以使用以下实例方法关闭s:

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

IE

[alert dismissWithClickedButtonIndex:0 animated:YES];

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

UIAlertView 类参考

于 2012-11-22T21:57:29.210 回答