从App Delegate检查屏幕上的UIAlertViews

时间:2015-09-24 07:51:29

标签: ios objective-c uialertview appdelegate

我的UIAlertView存在问题。

在我的AppDelegate中,我检查应用程序的可访问性: 如果无法访问,我会从Utils类调用警报。

- (void)reachabilityChanged:(NSNotification *)note
{
    Reachability* currentReachabilityObject = [note object];
    NSParameterAssert([currentReachabilityObject isKindOfClass:[Reachability class]]);
    NetworkStatus status = [currentReachabilityObject currentReachabilityStatus];

    if (status == NotReachable)
    {
        [Utils showAlert:@"NotReachableNetwork") title:@"Error")];
    }
}

如果我打开/关闭Wi-Fi两到三次,我会收到三个警报。 但我想只展示一个。

请告诉我如何检查AppDelegate屏幕上是否有任何警报。

2 个答案:

答案 0 :(得分:3)

为什么不保留对警报的引用?

这样你只需要检查警报是否为零,如果它是零,你可以创建一个新的警报。如果它不是零,则表示您已经有一个显示,并且不需要显示另一个。很容易就是馅饼。

答案 1 :(得分:0)

请尝试下面的代码,我认为它适合您。

#pragma mark - Internet Reachability Handlers -
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];

    if (_changeReachability)
    {
        if(netStatus==NotReachable)
        {
           [Utils showAlert:@"NotReachableNetwork") title:@"Error")];
            _isNetAvailable = NO;
            _changeReachability = NO;
        }
        else
        {
            _isNetAvailable = YES;
            _changeReachability = NO;
        }
    }
}

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
    _changeReachability = YES;
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability: curReach];
}

-(void)checkallTypesofInternet
{
    // For 3G Connection
    hostReach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    [hostReach startNotifier];
    [self updateInterfaceWithReachability: hostReach];

    // For Individual Net Connection
    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    [self updateInterfaceWithReachability: internetReach];

    // For WiFi
    wifiReach = [Reachability reachabilityForLocalWiFi];
    [wifiReach startNotifier];
    [self updateInterfaceWithReachability: wifiReach];
}

如果您仍然面临任何问题,请与我们联系。