objective-cuialertviewuialertcontrolleruialertviewdelegate

The Alert View shows many time not one time


I create UIAlertView in my function, the problem is it shows a lot of time when the function runs, how can I create an if statement to show only one time like if UIAlertView shows not show any more.

- (void)showAlert {
    
    _myAlertView = nil;
    _myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Call_On_Hold",nil)
                                              message:NSLocalizedString(@"Please_Wait",nil)
                                             delegate:self
                                    cancelButtonTitle:NSLocalizedString(@"Close_call",nil)
                                    otherButtonTitles:nil, nil];
    _myAlertView.tag = myAlertViewsTag;
    
    [_myAlertView show];
    
}

Here is the function that my UIAlertView appear continuously instead of one time.

- (void) trafficTimerRun:(NSTimer*)theTimer
{
    ++ trafficTimerTicks;
    
    pjmedia_rtcp_stat stat;
    
    if (!get_stream_info([call_id intValue], &stat)) {
        return;
    }
    
    LogDebug(TAG_SIP, @"Got %d bytes on stream 0 (previous: %d)", stat.rx.bytes, prev_bytes);
    
    if (stat.rx.bytes == prev_bytes) {
        if (trafficTimerTicks >= 10) {

            // Steve-note: Here we need to show a pop-up message when the call in on hold when the trafficTimerTicks run.
            [self showAlert];
            
            LogError(TAG_SIP, @"No traffic received, hanging up");

            // [theTimer invalidate];
            // broken = YES; Steve note: The call shouldnt broke.
            // [self hangup]; Steve note: The call shouldnt hangup.
        }
    }
}

Solution

  • Use a boolean:

    bool alertIsShowing = false;
    

    and in your updating method put something like this:

    if (trafficTicks > 10){
     if (!alertIsShowing){
         alertIsShowing = true;
         [self showAlert];
       }
    }
    

    Then when your alert is dismissed, reset your boolean:

    alertIsShowing = false;