两种方法无论何时同时被调用,如何解决?

时间:2014-02-19 18:37:17

标签: ios objective-c uialertview reachability

·H

- (void)checkForWIFIConnection;

的.m

- (void)checkForWIFIConnection {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No WIFI available!", @"AlertView")
                                                            message:NSLocalizedString(@"You have no wifi connection available. Please connect to a WIFI network.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Cancel", @"AlertView")
                                                  otherButtonTitles:NSLocalizedString(@"Open settings", @"AlertView"), nil];
        [alertView show];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self checkForWIFIConnection]; // this does not show an alert...
    [self parseXML]; // ...if this is here, but if i remove this line i get the UIALert saying no INTERNET

...
}

如果checkForInternetConnection没有失败,我如何让他只解析?

我认为这就像自我Checkforwifi ... {做这个}否则{做那个}

但我的checkforwifi无效,并没有返回BOOL,我试图改变方法,但因为我有点新,我失败了。

任何帮助?

干杯

3 个答案:

答案 0 :(得分:0)

在.h中,将其更改为:

- (BOOL)checkForWIFIConnection;

在你的.m中,将其更改为:

- (BOOL)checkForWIFIConnection

然后在.m文件中,在方法的最后,添加:

return netStatus == ReachableViaWiFi;

然后,您可以使用if方法设置viewDidLoad语句。

答案 1 :(得分:0)

简单。让checkForWIFIConnection方法返回结果:

- (BOOL)checkForWIFIConnection {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No WIFI available!", @"AlertView")
                                                            message:NSLocalizedString(@"You have no wifi connection available. Please connect to a WIFI network.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Cancel", @"AlertView")
                                                  otherButtonTitles:NSLocalizedString(@"Open settings", @"AlertView"), nil];
        [alertView show];

        return NO;
    } else {
        return YES:
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([self checkForWIFIConnection]) {
        [self parseXML];
    }

    ...
}

您无需在.h文件中声明checkForWIFIConnection。这是其他类不需要的私有方法。

答案 2 :(得分:0)

- (BOOL)checkForWIFIConnection {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    if (netStatus!=ReachableViaWiFi)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No WIFI available!", @"AlertView")
                                                            message:NSLocalizedString(@"You have no wifi connection available. Please connect to a WIFI network.", @"AlertView")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Cancel", @"AlertView")
                                                  otherButtonTitles:NSLocalizedString(@"Open settings", @"AlertView"), nil];
        [alertView show];
        return NO:
    }
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if([self checkForWIFIConnection]) {
        [self parseXML]; // ...if this is here, but if i remove this line i get the UIALert saying no INTERNET
    }

}

返回状态(BOOL)并在调用parseXML之前检查它,如上所示。