The code below is used to push another view controller onto the navigation stack.
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.
However, using performSegueWithIdentifier
causes no delay.
The viewDidLoad
code for SendViewController
is the same for the first and subsequent pushes.
Tried blanking out viewDidLoad
for the destination view controller, but still the lag exists for instantiateViewControllerWithIdentifier
but not for performSegueWithIdentifier
.
How to fix the delay with instantiateViewControllerWithIdentifier
?
No delay:
@IBAction func buttonTapped(sender: UIButton) {
performSegueWithIdentifier(SendSegue, sender: self)
}
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)
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…