UIAlertController仅在2秒后解除

时间:2014-12-10 09:00:16

标签: ios objective-c ipad

我希望在呈现后立即解雇,但只能在2秒后才能解决。怎么做?

这是我在UILabel上从UITapGestureRecognizer调用的方法。

- (IBAction)labelTaped:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateEnded) {
    CGRect frame = CGRectNull;
    NSString *message = nil;
    // ...
    // some code
    /// ...
    if (message) {
        // show info alert
        __weak __typeof(self)weakSelf = self;
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"alert_ok", @" - ")
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * action) {
                                                                 if ([weakSelf.dismissAlertTimer isValid]) {
                                                                     [weakSelf.dismissAlertTimer invalidate];
                                                                 }
                                                             }];
        [alert addAction:cancelAction];

        UIPopoverPresentationController *popoverController = alert.popoverPresentationController;
        popoverController.sourceRect = frame;
        popoverController.sourceView = sender.view;


        [self.mainController presentViewController:alert animated:YES completion:^{
            if ([weakSelf.dismissAlertTimer isValid]) {
                [weakSelf.dismissAlertTimer invalidate];
            }
            weakSelf.dismissAlertTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:weakSelf selector:@selector(dismissAlertController) userInfo:nil repeats:NO];
        }];
    }
  }
}

- (void)dismissAlertController {
    [self.mainController dismissViewControllerAnimated:YES completion:^{
        //
    }];
}

3 个答案:

答案 0 :(得分:9)

最简单的是这样吗?

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"message dismiss in 2 seconds" preferredStyle:UIAlertControllerStyleAlert];

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

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    [alertController dismissViewControllerAnimated:YES completion:^{
        // do something ?
    }];

});

或者你的意思是你还想阻止用户在触发2秒之前点击取消按钮?

答案 1 :(得分:3)

这应该可行。它对我有用

let alert = UIAlertController(title: "some title",
            message: "some message",
            preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)

        dispatch_async(dispatch_get_main_queue()) { () -> Void in

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in
                alert.dismissViewControllerAnimated(true, completion: nil)
            })
        }

答案 2 :(得分:0)

我通过使用扩展程序解除UIAlertController的超时问题。 这是代码。

extension UIViewController {
  func dismissViewController(timer: Timer) {
    let viewController = timer.userInfo as! UIViewController
    if (viewController.isViewLoaded && (viewController.view.window != nil)) {
        viewController.dismiss(animated: true, completion: nil)
    }
  }

  func presentWithTOT(_ viewControllerToPresent: UIViewController, timeout: Double) {
    if (timeout != 0.0) {
        Timer.scheduledTimer(timeInterval: timeout, target: self, selector: #selector(dismissViewController), userInfo: viewControllerToPresent, repeats: false)
    }
    present(viewControllerToPresent, animated: true, completion: nil)
  }
}

func myfunc() {
  let alertController = UIAlertController(...)
  presentWithTOT(alertController, timeout: 2.0)
}

上面代码的一个注意事项是,计时器拥有对alertController的强引用,这意味着在超时之前不会释放该对象。