Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Controller Loads Twice - How Do I Fix It?

Tags:

xcode

ios

xml

The Problem Cause

When I added a new Swift class file to my iOS project, I decided I was indecisive and renamed it, deleted it, and created a new file. I think that I was actually so indecisive that I renamed it a few times.

The Problem

Now that viewController loads twice. I can literally see the transition happen twice and, during testing, I can see my viewDidLoad run twice in my console.

Common Fix

I know the first solution to check for is duplicate segues / segue calls. Check your document outline to see if there's more than one segue. In my case, it is definitely not a segue problem in the storyboard or the viewController.

My Uncommon Situation

I found someone who had the same load-twice problem and it was because of the rename:

Screenshot

They said it was the renaming I did that is at the root of my problem and it is not easy to fix because the duplication is now in my XML file. But where is this XML file and how do I edit it to erase the original instance of the class? I have no idea.

So how do I solve this loads-twice issue? I just want my viewController to have a normal segue!

like image 760
Dave G Avatar asked Jan 07 '23 16:01

Dave G


1 Answers

Resolved my issue!

If you messed around with a class's name (or it's file name) and have the same "loads twice" issue that I was having, you might be having a hard time solving it. The only suggested solutions I found said to create a new project from scratch and copy stuff over. BUT, instead...

Here's the simplest solution ever:

Rename the class of the viewController that's loading twice! Don't worry about the file, but the view controller class within it. Your XML file probably has duplicates of the original class name, but rename your class to something entirely new and you should be okay.

like image 189
Dave G Avatar answered Jan 14 '23 23:01

Dave G