开发者

Why is my alert displayed twice?

in my app I check the current connection status to my website and the network status on app startup:

[[NSNotificationCenter defaultCen开发者_如何学Goter] addObserver:self 
       selector:@selector(reachabilityChanged:)
       name:kReachabilityChangedNotification object:nil];

internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];

hostReach = [[Reachability reachabilityWithHostName:@"www.google.de"] 
              retain];
[hostReach startNotifier];

[self updateInterfaceWithReachability:internetReach];
[self updateInterfaceWithReachability:hostReach];

//....

-(void)reachabilityChanged: (NSNotification* )note{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability: curReach];
}

In my updateInterfaceWithReachability I want to show an alert if a connection to the website cannot be established.

My problem is that the alert is displayed twice, so the first comes up and when I dismiss it the seconds is displayed:

if(curReach == hostReach){
        NetworkStatus netStatus = [curReach currentReachabilityStatus];

        if(netStatus != NotReachable){
            statusLabel.text = @"connected";
            [self setStatusColorGreen];
        }else{
            if(![internetReach connectionRequired]){
                statusLabel.text = @"not connected";
                compose.enabled = NO;
                [self doSMSFAlert];
                [self setStatusColorRed];
            }
        }
    }

Any ideas?

EDIT: I noticed now that I have a working host the alert is displayed too but it shouldn't. However the status is 'connected'


Because you are doing two Reachability notification. Your first one is for the Internet in general. The second one is for a specific host. Why don't you just do the host Reachability only? That way you will only get one alert.


You are responding to a notification event. The event can be delivered as many times as the status changes and it is up to you to know if you already have responded or are responding to a similar status change. Some options are storing a boolean or checking if you alert view is currently up before attempting to display another.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜