viewDidLoad 和 viewWillAppear 之间的 Swift 长延迟 [英] Swift Long Delay Between viewDidLoad and viewWillAppear
问题描述
我遇到了一个问题,我在尝试呈现 ViewController
时有很长的延迟.当用户单击需要高级访问权限的 UITableViewCell
时,我试图显示升级警报.在呈现的 ViewController
中,我放置了调试代码:
I'm encountering an problem where I am having a long delay when trying to present a ViewController
. I am trying to display an upgrade alert when a user clicks on a UITableViewCell
which requires premium access. In the ViewController
being presented, I put debug code:
override func viewDidLoad() {
super.viewDidLoad()
println("\(NSDate()) viewDidLoad")
// Set Navigation Title font and color
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "UbuntuCondensed-Regular", size: 22)!,
NSForegroundColorAttributeName: UIColor.whiteColor()]
println("\(NSDate()) end of viewDidLoad")
}
override func viewWillAppear(animated: Bool) {
println("\(NSDate()) before super.viewWillAppear(animated)")
super.viewWillAppear(animated)
println("\(NSDate()) after super.viewWillAppear(animated)")
}
override func viewDidAppear(animated: Bool) {
println("\(NSDate()) before super.viewDidAppear(animated)")
super.viewDidAppear(animated)
println("\(NSDate()) after super.viewDidAppear(animated)")
}
println
语句导致:
2015-06-23 16:36:54 +0000 viewDidLoad
2015-06-23 16:36:54 +0000 end of viewDidLoad
2015-06-23 16:36:57 +0000 before super.viewWillAppear(animated)
2015-06-23 16:36:57 +0000 after super.viewWillAppear(animated)
2015-06-23 16:36:58 +0000 before super.viewDidAppear(animated)
2015-06-23 16:36:58 +0000 after super.viewDidAppear(animated)
如您所见,在 viewDidLoad
结束和 viewWillAppear
开始之间有 3 秒的延迟.我无法弄清楚为什么会发生这种情况.我在 ViewController
中以编程方式创建视图,所以这里没有使用故事板.
As you can see there is a 3 second delay between the end of viewDidLoad
and the start of viewWillAppear
. I can't figure out why this is occurring. I am creating the views programmatically within the ViewController
, so the storyboard is not being used here.
这是我必须展示我的ViewController
的代码:
This is the code I have to present my ViewController
:
// Create the upgrade view contorller
let upgradeVC = UpgradeViewController()
// Set the presentation context
self.providesPresentationContextTransitionStyle = true
self.definesPresentationContext = true
// Set the upgrade controller to be modal over current context
upgradeVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
// Show the view controller
self.navigationController?.presentViewController(upgradeVC, animated: true, completion: nil)
推荐答案
创建和呈现视图控制器应该在主线程上完成.
Creating and presenting the view controller should be done on the main thread.
(当视图更新延迟时,几乎总是意味着您错误地在后台线程上工作.)
(When view updates are delayed, it nearly always means you're erroneously working on a background thread.)
这篇关于viewDidLoad 和 viewWillAppear 之间的 Swift 长延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!