2

我有一个 ViewController(ViewController.m,它通过 web 服务加载数据。这些方法包含在 GetHTTP.m 中。

在 GetHTTP.m 中,我已包含在Reachable.hInternet 连接断开时收到通知。

[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(checkNetworkStatus:) 
       name:kReachabilityChangedNotification object:nil];

但是如何从我的 GetHTTP::checkNetworkStatus 方法中发出警报,因为所有视图都在我的 ViewController.m 类中?

可能我可以在 ViewController 类中使用一个方法作为我的选择器的目标,但是我从多个 ViewController 调用 GetHTTP,因此每次从另一个 ViewClass 调用选择器目标时都必须更改它。

也许我错过了一个简单的 MVC 规则?

提前致谢。基督教

4

2 回答 2

1

使用此代码发出警报消息

-(void) checkNetworkStatus:(NSNotification *)notice
{
    recheabilityBool=FALSE;
    nonrecheabilityBool=FALSE;
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            nonrecheabilityBool=TRUE;

            NSLog(@"The internet is down.");
            [self checkAndCreateDatabase];
            [self readLikeDislikeFromSyncDB];

            UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Please connect to the internet to experience all the features of this app." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];  
            [myAlert show];  
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            Con=0;
            [prefs setInteger:Con forKey:@"conKey"];
            UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:self.libraryViewController];
            [navCon setNavigationBarHidden:YES];
            [[self window] setRootViewController:navCon];

            break;
        }
        case ReachableViaWiFi:
        {


            recheabilityBool=TRUE;
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            Con=404;
            [prefs setInteger:Con forKey:@"conKey"];
            [self performSelectorOnMainThread:@selector(storeLikeFromCTB) withObject:nil waitUntilDone:YES];
            // [self deleteAllRecordsDB];



            //  [self performSelector:@selector(storeDisLikeFromCTB) withObject:nil afterDelay:0];
            //[self performSelector:@selector(GetApps) onThread:[self myThread] withObject:nil waitUntilDone:YES];
            //[self performSelector:@selector(GetApps) withObject:nil afterDelay:0];
            [self performSelectorOnMainThread:@selector(updateToCTB) withObject:nil waitUntilDone:YES];


            [self performSelectorOnMainThread:@selector(GetApps) withObject:nil waitUntilDone:YES];            
            //[self performSelector:@selector(gotoHome) withObject:nil afterDelay:0];

            NSLog(@"The internet is working via WIFI.");


            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            if( nonrecheabilityBool==FALSE)
            {
                [self checkAndCreateDatabase];
                [self readLikeDislikeFromSyncDB];           
                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                Con=0;
                [prefs setInteger:Con forKey:@"conKey"];
                UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:self.libraryViewController];
                [navCon setNavigationBarHidden:YES];
                [[self window] setRootViewController:navCon];

                NSLog(@"A gateway to the host server is down.");
            }          
            break;

        }
        case ReachableViaWiFi:
        {
            if(recheabilityBool==FALSE)
            {

                recheabilityBool=TRUE;

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                Con=404;
                [prefs setInteger:Con forKey:@"conKey"];
                [self performSelectorOnMainThread:@selector(storeLikeFromCTB) withObject:nil waitUntilDone:YES];
                //  [self performSelector:@selector(storeDisLikeFromCTB) withObject:nil afterDelay:0];
                //[self performSelector:@selector(GetApps) onThread:[self myThread] withObject:nil waitUntilDone:YES];
                //[self performSelector:@selector(GetApps) withObject:nil afterDelay:0];
                [self performSelectorOnMainThread:@selector(updateToCTB) withObject:nil waitUntilDone:YES];


                [self performSelectorOnMainThread:@selector(GetApps) withObject:nil waitUntilDone:YES];            
                //[self performSelector:@selector(gotoHome) withObject:nil afterDelay:0];

                NSLog(@"The internet is working via WIFI.");
                break;
            }


            NSLog(@"A gateway to the host server is working via WIFI.");

            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            break;
        }
    }
}
于 2012-11-08T10:36:40.600 回答
0
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil];

    -(void)checkForReachability{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];

    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
        //Do something
    }
     else if (remoteHostStatus == ReachableViaWiFi) {
    // Do something
     }

    else{

    // Else do something else
    }
}
于 2012-11-08T09:33:05.720 回答