延迟使用InstantiateViewControllerWithIdentifier而不执行performSegueWithIdentifier吗? [英] Delay when using instantiateViewControllerWithIdentifier but not performSegueWithIdentifier?

查看:52
本文介绍了延迟使用InstantiateViewControllerWithIdentifier而不执行performSegueWithIdentifier吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码用于将另一个视图控制器推入导航堆栈.

The code below is used to push another view controller onto the navigation stack.

使用instantiateViewControllerWithIdentifier时,第一次的速度明显很慢(〜3秒),但随后的每次速度都相当快.其他SO职位建议确保序号发生在代码完成的主线程上,但这不能解决问题.

When using instantiateViewControllerWithIdentifier, the segue is noticeably sluggish the first time (~3 seconds) but occurs reasonably fast each subsequent time. Other SO posts suggested ensuring the segue occurs on the main thread, which the code accomplishes, but this didn't fix the problem.

但是,使用performSegueWithIdentifier不会引起延迟.

However, using performSegueWithIdentifier causes no delay.

SendViewControllerviewDidLoad代码在第一次和随后的推送中都是相同的.

The viewDidLoad code for SendViewController is the same for the first and subsequent pushes.

尝试将目标视图控制器的viewDidLoad消隐,但对于instantiateViewControllerWithIdentifier仍然存在滞后,对于performSegueWithIdentifier则不存在.

Tried blanking out viewDidLoad for the destination view controller, but still the lag exists for instantiateViewControllerWithIdentifier but not for performSegueWithIdentifier.

如何使用instantiateViewControllerWithIdentifier修复延迟?

没有延迟:

@IBAction func buttonTapped(sender: UIButton) {
    performSegueWithIdentifier(SendSegue, sender: self)  
}

首次显示SendViewController时会导致延迟:

Results in delay when showing SendViewController for first time:

@IBAction func buttonTapped(sender: UIButton) {
    dispatch_async(dispatch_get_main_queue()) {
        let vc = self.storyboard!.instantiateViewControllerWithIdentifier(self.SendViewControllerID) as! SendViewController
        self.navigationController!.pushViewController(vc, animated: true)
    }   
}

推荐答案

问题被隔离到目标视图控制器中存在UITextField,也就是说,删除UITextField可以消除滞后.

The problem was isolated to the presence of a UITextField in the destination view controller, that is, removing the UITextField removes the lag.

然后将其进一步隔离为存在自定义字体.

Then it was further isolated to the presence of a custom font.

换句话说,使用UITextField上的系统字体而不是自定义字体可以消除延迟.没有解释为什么,但是有效.

In other words, using the system font on the UITextField, rather than a custom font, removes the lag. No explanation why, but it works.

这篇关于延迟使用InstantiateViewControllerWithIdentifier而不执行performSegueWithIdentifier吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆