无论如何,这两种方法同时被调用,如何解决?
Posted
技术标签:
【中文标题】无论如何,这两种方法同时被调用,如何解决?【英文标题】:Both methods being called at the same time no matter what, how to fix it? 【发布时间】:2014-02-19 18:37:16 【问题描述】:.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 没有失败,我如何让他只解析?
我认为这就像 if self Checkforwifi... do this else do that
但是我的 checkforwifi 是无效的,并且不返回 BOOL,我尝试更改方法,但由于我有点新,所以我失败了。
有什么帮助吗?
干杯
【问题讨论】:
【参考方案1】:- (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 之前检查它,如上所示。
【讨论】:
【参考方案2】:在您的 .h 中,将其更改为:
- (BOOL)checkForWIFIConnection;
在你的 .m 中,将其更改为:
- (BOOL)checkForWIFIConnection
然后也在.m文件中,在方法的最后,添加:
return netStatus == ReachableViaWiFi;
然后您可以在viewDidLoad
方法中设置您的if
语句。
【讨论】:
【参考方案3】:简单。让您的 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
。这是其他类不需要的私有方法。
【讨论】:
以上是关于无论如何,这两种方法同时被调用,如何解决?的主要内容,如果未能解决你的问题,请参考以下文章