Is there a way to dismiss an no button UIalertView after some time?
Asked Answered
A

4

13
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"tittle"
               message:@""
                 delegate:self
              cancelButtonTitle:@""
              otherButtonTitles:nil];
  [alertView show];
  [alertView release];

i want to dimiss the alerview after it's showing for some time,but when the alertview has no button,it doesn't work if i invoked -dismissWithClickedButtonIndex:animated: methodand-performSelector:withObject:afterDelay: is there any other way to dismiss it ? thanks for any ideas!

Aramaic answered 16/11, 2010 at 9:16 Comment(0)
A
26
-(void)xx  {
     [self performSelector:@selector(dismissAlertView:) withObject:alertView afterDelay:2];
}
-(void)dismissAlertView:(UIAlertView *)alertView{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}

that's it.i fix it

Aramaic answered 16/11, 2010 at 9:25 Comment(1)
Ben, if the cancelButton of the alertView is set to "nil", how will the "[alertView dismissWithClickedButtonIndex:0 animated:YES];" thing work??? I tried that and it worked, but cant figure out how.... Any help is appreciated! Thank you!Sulphuryl
P
2

Use 10 seconds delays to dismiss

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [alertController dismissViewControllerAnimated:YES completion:^{
        p\Perform Action after dismiss alertViewController
    }];
});
Penner answered 9/6, 2016 at 10:35 Comment(0)
P
1

In Xamarin.iOS / Monotouch this worked for me:

private async Task ShowToast(string message, UIAlertView toast = null)
    {
        if (null == toast)
        {
            toast = new UIAlertView(null, message, null, null, null);
            toast.Show();
            await Task.Delay(2000);
            await ShowToast(message, toast);
            return;
        }

        UIView.BeginAnimations("");
        toast.Alpha = 0;
        UIView.CommitAnimations();
        toast.DismissWithClickedButtonIndex(0, true);
    }

Remember that if an async method is called from a background thread (not the main UI thread) then InvokeOnMainThread would still be required. This just means that you call the above method like this:

 BeginInvokeOnMainThread(() =>
 {
   ShowToast(message);
 });
Pessary answered 16/2, 2016 at 12:23 Comment(0)
M
0

update to the answer above as UIAlertView is deprecated Answer At this Link

the 2nd function should be like this

-(void)dismissAlertView:(UIAlertController *)alertView{

  [alertView dismissViewControllerAnimated:YES completion:nil];
}
Mcmillon answered 6/4, 2016 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.