【问题标题】:Is there a way to dismiss an no button UIalertView after some time?有没有办法在一段时间后关闭无按钮 UIalertView?
【发布时间】:2011-05-10 16:55:35
【问题描述】:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"tittle"
               message:@""
                 delegate:self
              cancelButtonTitle:@""
              otherButtonTitles:nil];
  [alertView show];
  [alertView release];

我想在alerview显示一段时间后关闭它,但是当alerview没有按钮时,如果我调用-dismissWithClickedButtonIndex:animated: methodand-performSelector:withObject:afterDelay:它不起作用 有没有其他方法可以解除它? 感谢您的任何想法!

【问题讨论】:

    标签: ios objective-c uialertview dismiss


    【解决方案1】:
    -(void)xx  {
         [self performSelector:@selector(dismissAlertView:) withObject:alertView afterDelay:2];
    }
    -(void)dismissAlertView:(UIAlertView *)alertView{
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
    

    就是这样。我修复它

    【讨论】:

    • 本,如果alertView的cancelButton设置为“nil”,“[alertViewdismissWithClickedButtonIndex:0 animated:YES];”怎么办?东西工作???我试过了,它奏效了,但不知道如何......感谢任何帮助!谢谢!
    【解决方案2】:

    使用 10 秒延迟关闭

    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
        }];
    });
    

    【讨论】:

      【解决方案3】:

      在 Xamarin.iOS / Monotouch 这对我有用:

      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);
          }
      

      请记住,如果从后台线程(而不是主 UI 线程)调用异步方法,则仍然需要 InvokeOnMainThread。 这只是意味着您像这样调用上述方法:

       BeginInvokeOnMainThread(() =>
       {
         ShowToast(message);
       });
      

      【讨论】:

        【解决方案4】:

        更新上述答案为UIAlertViewdeprecated Answer At this Link

        第二个函数应该是这样的

        -(void)dismissAlertView:(UIAlertController *)alertView{
        
          [alertView dismissViewControllerAnimated:YES completion:nil];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多