user7515581
user7515581

Reputation:

UIAlertController: make disappear a message after 3 seconds

I wanted to know how to make disappear a message after 3 seconds ? Thanks,

    UIAlertController *alert_carbu_reglementaire = [UIAlertController
                              alertControllerWithTitle:@"ATTENTION"
                              message:@"Le résultat ne prend pas en compte la quantité réglementaire"
                              preferredStyle:UIAlertControllerStyleAlert];

     [self presentViewController:alert_carbu_reglementaire animated:YES completion:nil];

Upvotes: 0

Views: 502

Answers (2)

LLIAJLbHOu
LLIAJLbHOu

Reputation: 1313

Try to use GC power to call setMessage after 3 sec. Look at this example:

Swift 2.3

let delay = 3.0
let delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))

dispatch_after(delayInNanoSeconds, dispatch_get_main_queue()) {
    alert_carbu_reglementaire.message = ""
}

Objective-C

double delay = 2.0;
dispatch_time_t delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [alert_carbu_reglementaire setMessage:@""];
});

Be sure that you don't create memory leak.

Upvotes: 0

danh
danh

Reputation: 62676

There are two useful tools at your disposal: the first is that you can easily delay execution with one of the performSelector methods like this:

// after present, wait 3 seconds and perform a selector
[self presentViewController:alert_carbu_reglementaire animated:YES completion:nil];
[self performSelector:@selector(dismissTheAlert) withObject:nil afterDelay:3.0];

The second is that fact that view controllers may dismiss vcs presented atop them (and anything presented above those) with dismisViewController...

- (void) dismissTheAlert {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

Upvotes: 3

Related Questions