使用instantiateViewControllerWithIdentifier 但不使用performSegueWithIdentifier 时延迟? [英] Delay when using instantiateViewControllerWithIdentifier but not performSegueWithIdentifier?
问题描述
下面的代码用于将另一个视图控制器推送到导航堆栈上.
The code below is used to push another view controller onto the navigation stack.
当使用 instantiateViewControllerWithIdentifier
时,segue 在第一次(约 3 秒)时明显缓慢,但随后每次都相当快.其他 SO 帖子建议确保 segue 发生在代码完成的主线程上,但这并没有解决问题.
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.
SendViewController
的 viewDidLoad
代码对于第一次和后续推送是相同的.
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
修复延迟?
How to fix the delay with 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屋!