【问题标题】:Add Delay to PresentViewController向 PresentViewController 添加延迟
【发布时间】:2015-09-07 04:23:41
【问题描述】:

我想知道如何在我的代码中添加 1.0 秒的延迟:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
[self presentViewController:homeViewController animated:YES completion:nil];

我知道你可以这样做:

[self performSelector:@selector(showModalTwo:)withObject:someNumber afterDelay:1.0f];

我只是没有这个或想创建一个函数来进行 segue。任何帮助都会很棒。谢谢!

【问题讨论】:

  • 延迟后是否要执行segue?还是什么?
  • 是的 - 我只是希望在延迟时间之后发生这种情况。我有一个登录,所以我想一旦我给它一个延迟,这会给登录足够的时间来获取我需要用这个 segue 传输的信息

标签: objective-c xcode uiviewcontroller segue viewcontroller


【解决方案1】:

不建议为等待操作完成(即登录)添加延迟,您可以使用 Grand Central Dispatch

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        //Background Thread
        // do you login logic here

        dispatch_async(dispatch_get_main_queue(), ^(void){
            //Main Thread : UI Updates
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
            [self presentViewController:homeViewController animated:YES completion:nil];

        });
    });

【讨论】:

  • 这很好用 - 感谢您的帮助。我很感激!
  • 这不是实现这种延迟的好方法。如果您只是想延迟演示,请使用dispatch_after
  • 其实他不需要延迟,登录成功后需要在当前视图控制器后台进行登录。如果他延迟使用 dispatch_after,他将无法确定登录是否完成
猜你喜欢
  • 1970-01-01
  • 2017-10-29
  • 2019-12-15
  • 2011-09-20
  • 2023-04-05
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2014-11-09
相关资源
最近更新 更多