0

デバイスがインターネットに接続されているかどうかを確認しようとしています。そうでない場合は、アラートがポップアップし、ユーザーは「再試行」を押して再試行する必要があります。私のコードでは、最初にアラート ビューがポップアップしたときに「再試行」を押してから、2 回目にアラートがポップアップしたときに、1 秒後に自動的に閉じます。しかし、「再試行」を押したときにのみ却下されると思われます。

これが私のコードです:

.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

私の英語で申し訳ありませんが、よろしくお願いします!!!

4

1 に答える 1

0

あなたが見ている問題を再現することができました。それは、基本的に、alertView がまだ画面に表示されていて、閉じられているときに表示するように指示しているという事実に関係しています。alertView が閉じられるまでに少し時間がかかり、[再試行] ボタンを押すと、同じアラート ビューが閉じられ、表示されます。

これを修正するには、アラート ビュー変数を削除します。この場合、それを保持する必要はありません。簡単な方法でメッセージを表示し、いつでも呼び出すことができます。また、アラートは毎回別のインスタンスであるため、同時に表示/非表示にすることを心配する必要はありません。

- (void)showInternetConnectionMessage {
    [[[UIAlertView alloc]initWithTitle: @"Unstable Network Connection" message: @"Unstable network connection." delegate: self cancelButtonTitle: nil otherButtonTitles: @"Try Again", nil] show];
}
于 2014-08-25T14:26:13.210 に答える