0

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.
        }
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Steven
  • 762
  • 1
  • 10
  • 27

1 Answers1

0

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;
Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
  • Hi @Johnny Rockex, my UIAlertView dismiss automatically, I tried but it doesn't work. – Steven May 12 '17 at 10:21
  • Do you want it to dismiss automatically without user interaction? Is our alertView not calling a delegate method on dismissal? – Johnny Rockex May 12 '17 at 10:25
  • No sorry for misunderstanding, I have already wrote the function to dismiss the UIAlertview, but th sonly things I want that the Alertview has to show one time not 100 times. :( – Steven May 12 '17 at 10:37
  • you can use a boolean for that too, bool hasAlertShown = false; then when it shows, you set it to true and check that when the next alert *might* show. – Johnny Rockex May 12 '17 at 12:44