Delay when using instantiateViewControllerWithIdentifier but not performSegueWithIdentifier?(使用 instantiateViewControllerWithIdentifier 但没有 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 时延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 instantiateViewControllerWithIdentifier 但没有 performSegueWithIdentifier 时延迟?
基础教程推荐
- Play 商店的设备兼容性问题 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
