iOS*_*eer 6 objective-c ipad ios
我希望在呈现后立即解雇,但只能在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:^{
//
}];
}
Run Code Online (Sandbox Code Playgroud)
最简单的是这样的吗?
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 ?
}];
});
Run Code Online (Sandbox Code Playgroud)
或者你的意思是你还想阻止用户在触发2秒之前点击取消按钮?
| 归档时间: |
|
| 查看次数: |
5700 次 |
| 最近记录: |