UIAlertView自行解散(不要这个)Xcode

时间:2014-08-25 13:49:23

标签: ios objective-c xcode networking alertview

我正在尝试检查设备是否已连接到互联网。如果没有,则会弹出警报,用户需要按“再试一次”再试一次。 在我的代码中,第一次弹出警报视图时,我按“再试一次”,然后第二次弹出警报,它会在一秒钟后自行解除。 但是,当我按下“再试一次”时,我们只想解雇。

这是我的代码:

在.h:

#import <UIKit/UIKit.h>

@class Reachability;

@interface ViewController : UIViewController{
    Reachability* internetReachable;
    Reachability* hostReachable;

    UIAlertView *networkAlert;
}

-(void) checkNetworkStatus;

@end

在.m:

#import "ViewController.h"
#import "Reachability.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    networkAlert = [[UIAlertView alloc]initWithTitle: @"Unstable Network Connection"
                                         message: @"Unstable network connection."
                                        delegate: self
                               cancelButtonTitle: nil
                               otherButtonTitles: @"Try Again", nil];

    [self checkNetworkStatus];

 }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


 -(void) checkNetworkStatus
{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
         {
            NSLog(@"The internet is down.");
            [networkAlert show];
            break;
         }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");        
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSLog(@"user pressed Button Indexed 0");
        // Any action can be performed here
        [self checkNetworkStatus];

    }
}

@end

对不起我的英文,非常感谢你提前!!!

1 个答案:

答案 0 :(得分:0)

我能够复制您正在看到的问题,这与您实际上告诉alertView显示它何时仍在屏幕上以及在被解雇的过程中这一事实有关。 alertView被解除所需的时间稍有延迟,当他们点击再试一次按钮时,您都会解雇并显示相同的警报视图。

对此的修复是删除您拥有的警报视图变量,在这种情况下实际上没有必要保留它。使用一种简单的方法来显示消息,并随时调用它,并且因为它每次都是一个单独的警报实例,所以您不必担心同时显示/隐藏它。

- (void)showInternetConnectionMessage {
    [[[UIAlertView alloc]initWithTitle: @"Unstable Network Connection" message: @"Unstable network connection." delegate: self cancelButtonTitle: nil otherButtonTitles: @"Try Again", nil] show];
}