Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewController Appears to Transition Twice

Problem Summary

My mainVC --> second VC transtion appears to run twice. However, what's odd is that the second VC's viewDidLoad only runs once. This is the only transition in my project that has this problem.

I have researched this and found load-twice issues but none are a match to my transition twice but load once scenario. Common issues, like someone having added two segues in the storyboard, do not appear in my project:

enter image description here

In my first/root view controller (called mainVC) I programmatically add an edge pan gesture in the viewDidLoad:

        let panLeftEdgeGesture_MainVC = UIScreenEdgePanGestureRecognizer(target: self, action: "panLeftEdge_toLifeList:")
        panLeftEdgeGesture_MainVC.edges = .Left
        view.addGestureRecognizer(panLeftEdgeGesture_MainVC)

When the user swipes that pan gesture, it triggers a function that calls a segue to my next VC. Problem is, that VC transition appears twice and then everything works fine after that. Nothing breaks or crashes. If I click my custom back button, the unwind back to mainVC only appears once.

Experiment to Shed Light on the Cause

Here's the weird part: in an attempt to understand what was understand, I inserted some println code and here's what the console prints:

No matching prepareForSegue: So did nothing to prepare.
LifeList_CollectionView viewDidLoad did run
Pan segue from mainVC to Life Lists did run
No matching prepareForSegue: So did nothing to prepare.

That first line is the prepareForSegue running. It says "did nothing" because in my prepareForSegue it checks which segue is running and sometimes it'll pass some code. In this case, the prepareForSegue does nothing but print that line.

The second line is the second VC's viewDidLoad running.

The third line seems weird and out of order to me, but I'm no expert. That println is in the mainVC and prints from the function called by the pan gesture, this is the function that triggered the segue in the first place. Why would this not print before the 2nd VC's viewDidLoad?:

func panLeftEdge_toLifeList(sender: UIScreenEdgePanGestureRecognizer) {
    performSegueWithIdentifier("segueToLifeLists", sender: nil)
    println("Pan segue from mainVC to Life Lists did run")
}

The fourth line is the mainVC's prepareForSegue running a second time. However, I don't see the console printing a second viewDidLoad, even though visually I see the transition happen twice.

Any idea what could cause such an odd behavior or, at the least, any tricks to stop this behavior?

like image 438
Dave G Avatar asked Aug 29 '15 16:08

Dave G


People also ask

Can viewDidLoad be called multiple times?

viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once.

How do I create a segue between view controllers?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.


1 Answers

I had ALMOST the same problem and my second VC was sliding twice. The problem was with that segue arrow in my Main.storyboard. I control-dragged from a button in first VC and not from my VC (that little yellow circle on top of every VC), so I deleted that segue arrow and control-dragged from VC yellow circle to my second VC and it fixed the problem.

Also this is how I used performSegue function:

@IBAction func toSignUp(_ sender: Any) {
    performSegue(withIdentifier: "signUp", sender: nil)
}

Hope this was helpful.

like image 106
Saeed Hajizade Avatar answered Sep 29 '22 08:09

Saeed Hajizade