Why don't I have a ViewController.swift file to start with in XCode?

Hello, community, I've been having some issues lately with XCode (I'm am an absolute beginner in coding with swift and in developing apps). I have seen multiple tutorials and everyone seems to have a ViewController.swift file over or under their LaunchScreen.storyboard. When i try to drag buttons, labels, etc. to the assistant editor, i only have a UIViewController.h file, which cant be edited. As soon as i "created" a pseudo-ViewController.swift file of my own to replace the UIViewController.h file so that i could actually alter some functions, XCode began to have critical erros.


Images:

1) This occurs as soon as i replace the UIViewController.h file

2) The start of a fresh new app (no viewcontroller.swift file)

3) If i check "Use as Launch Screen" the second error occurs

4) second error

5) When i check "is initial view Controller the third error occurs

6) third error









I just want to be able to make an app, but this problem is proving to be rather difficult for me. If someone could tell me how to replace the UIViewController.h file with the normal ViewController.swift file, i would greatly appreciate it. As i am a beginner, i would also need a simplfied explaination to resolving the issue. Thank you very much!

Answered by sinandraide in 388112022

For some reason my images seem to have been removed from the post. Is there any way that i can add them to the post? Maybe some important information: when i run the app it opens an older app of mine, which i have already deleted. Maybe i need to reinstall XCode?

Accepted Answer

For some reason my images seem to have been removed from the post. Is there any way that i can add them to the post? Maybe some important information: when i run the app it opens an older app of mine, which i have already deleted. Maybe i need to reinstall XCode?

Are you creating a new project, or trying to use an existing project? If this is a new project, did you change the programming language to Swift? What version of Xcode are you using?


With Xcode 11, I just created a new iOS Single App project with storyboards, using both Swift and Objective-C. In Swift, the project contains an AppDelegate.swift, SceneDelegate.swift, and ViewController.swift, the storyboard, and support files. In the Objective-C project, there were AppDelegate.h/.m, SceneDelegate.h/.m, and ViewController.h/.m, with storyboard, et al, support files.


I'm assuming that when you refer to "UIViewController.h," you mean "ViewController.h" which is a sub-class of UIViewController. If you want to replace ViewController.h with ViewController.swift, you have to get rid of both the .h and .m files.

Images never show in the posts on this forum. You can paste them but they are skipped out …


Don't think you have to reinstall XCode, but just check that the class given to the viewController (in Interface Builder Identity inspector) is the one defined in the swift file.

>images seem to have been removed from the post. Is there any way that i can add them to the post?


You can add them....we're not allowed to see them. See FAQs 1 & 2 here: For Best Results - Read the Label - for options.

If you're using Xcode 11, the most likely cause of your issue is you created a SwiftUI project. SwiftUI projects do not use view controllers so they do not have a ViewController.swift file. If you want a ViewController.swift file in your project, you must use storyboards instead of SwiftUI for the user interface. Choose Storyboard from the User Interface menu when you create the project and you should get a ViewController.swift file in your project.

Can you please tell me how to get rid off the .h and .m files?

I think I have the same issue as sinandraide.

When I try to open the "assistant" the ViewController.h file occures and I can't select the ViewController.swift file.

Thanks szymczyk,


this was very helpful and worked for me.

I solved this problem, when you creating a new project in Xcode 11 you should follow this steps:
  1. Create a "Single view App"

  2. In Field “User Interface” you have two options of choosing "SwiftUI" or "Storyboard. Choose "Storyboard" then the view controller file will be created automatically.

Hope it was helpful
If you're trying to follow an older tutorial that requires you to make a Single View App (that option isn't there in XCode anymore), you can just create an "App" with storyboard selected instead of swiftUI. That will get you close. If you want the exact folder/file structure of Single View App, you can use this github template as well: https://github.com/jcross0204/ios-single-view-app-template/tree/master
Why don't I have a ViewController.swift file to start with in XCode?
 
 
Q