2 lianlianbushell lianlianbushell 于 2012.11.23 10:06 提问

求助:如何禁用Alertview弹出

我的应用里面有一个Alertview,没有网络连接的时候会弹出来。但是我希望有网络连接的时候它可以自动禁用。代码如下:

-(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
                                                  otherButtonTitles:nil];
            UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
            progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
            [alert addSubview:progress];
            [progress startAnimating];
            [alert show];
        }
    }

1个回答

love_loveec
love_loveec   2012.11.23 10:57

可以把alertView当做实体变量,然后在ivar调用didDismissWithButtonIndex,然后就可以在viewDidLoad中分配alert,然后:

 -(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");

            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。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!