ViewController.swift and Cocoa Touch Class as a subclass of UIViewController - ios

I am following an iOS Swift tutorial to build an app. The instructor suggested that I delete the ViewController.swift file and add a new Cocoa Touch Class template which would be a subclass of UIViewController but did not explain why. Can someone enlighten me on the reasoning behind this and its benefits oppose to building the app in the original ViewController.swift file?
Is it just a customization method to naming your files?

The ViewController.swift file is created by default with the Single Application project template. An iOS project can have a lot ViewControllers.
View controllers are the foundation of your app’s internal structure. Every app has at least one view controller, and most apps have several. Each view controller manages a portion of your app’s user interface as well as the interactions between that interface and the underlying data. View controllers also facilitate transitions between different parts of your user interface.
For more information you can check Apple documentation: The Role of View Controllers

Related

Adding Multiple View Controllers to ViewController.swift From Storyboard UI

I have created several view controllers that I wish to now add tap actions to in my Main.storyboard, but they are not connected to the ViewController.swift. I am trying to connect the ViewControllers via the control-click-drag-to-swift-file method, but the object does not appear in the swift file. Is there a way to simply import your entire storyboard with all the ViewControllers into the swift file so you can start coding?
Thank you.
If I understand correctly, you have multiple view controllers all with the same class "ViewController" that you want to duplicate and edit all the same way? You have to label each class differently or else the system crashes its like having twins and naming them all the same name and getting confused why they dont know which one you are talking to.

how is view controller connected with App Delegate

I was trying to understand the flow of this app: https://github.com/JoelMarcey/iOS-2048
but I am not sure how this app works (absolute beginner)
So we have main.m which happens to be a starting point of the app?
https://github.com/JoelMarcey/iOS-2048/blob/master/NumberTileGame/NumberTileGame/main.m
main.m imports AppDelegate.h
https://github.com/JoelMarcey/iOS-2048/blob/master/NumberTileGame/NumberTileGame/F3HAppDelegate.h
AppDelegate.m does not import ViewController? So how does this App show initial screen and screen thereafter?
Shouldn't there be some code in AppDelegate.m? Can someone please explain me the code?
Also, how do I see Main_iPhone.storyboard in my project and not in repo online?
If you look in the NumberTileGame-Info.plist file, you'll see that there's a reference to a Storyboard. A storyboard is a file that the system uses to load UI elements such as view controllers, including (sometimes) the connections between the controllers.
In this app's case, the storyboard loads F3HViewController as the initial view controller. That's why you don't see a reference to the controller in the app delegate. Regarding your question of code in the app delegate, it's not necessary to have a bunch of code in the app delegate -- it's an architecture decision that determines what code goes in view controllers, the app delegate, etc.
Regarding your comment about the storyboard, the storyboard is in the repo -- it's in Base.lproj.
AppDelegate.m does not import ViewController? So how does this App show initial screen and screen thereafter?
This is a storyboard-based app. The Info.plist specifies the storyboard to use, and that storyboard specifies the initial scene, which is basically a graph of objects including a view controller and its view hierarchy.
If you open the storyboard file, you'll see an arrow pointing to the first scene, which will likely have an instance of F3HViewController. I haven't actually downloaded the project, but that class seems like the obvious candidate since it only has a -playGameButtonTapped action that presumably is connected to a "Play" button. That action then instantiates and presents the next view controller, F3HNumberTileGameViewController, and gameplay begins.
Shouldn't there be some code in AppDelegate.m? Can someone please explain me the code?
Only if you want to customize what the application does. In this case the developer doesn't seem to want that.
Also, how do I see Main_iPhone.storyboard in my project and not in repo online?
It's in the Base.lproj subdirectory.

Should Separate ViewController.Swift be Created for each Scene / Screen?

I'm new to iOS development but have used MVC frameworks on other platforms. In ready through the getting started documents about view controllers, it seems that there are several approaches in it's use.
I'm building an app that will have numerous pages / scenes. In using the storyboard, I'm creating a scene for each screen e.g. sign up, log in, home, and feeds to name a few. My question is should a separate view controller be created for each screen e.g. signupcontroller.swift, logincontroler.swift etc.? I started with the single screen project in XCode and have not been able to locate the automated way to create a new view controller for each scene. Is this indeed a manual effort, or is there a better starter projet type for a robust app such as this one?
Thanks much for your help and guidance.
You generally have a separate view controller class for each "scene," although it is often the case that several scenes will share a view controller with different data associated with them. Once you make the scene in the storyboard, you create a new UIViewController class and then associate it with a view controller in the storyboard...
Add a new Cocoa Touch class to the project
Name the view controller
Associate the view controller in the storyboard with the new class you created.

iOS Development: Is it necessary to change the class of the View Controller in the storyboard?

I am currently learning iOS development with Objective C. I am facing a couple of issues and I was wondering whether someone could give me some clarifications about xCode. I was reading a book on iOS and I was following it's instructions.
It was saying to create a header and an implementation file with the name test.h and test.m respectively. The test class will have UIViewController as superclass. This is all very fine...
Then, from the instructions, it says to drag out a View Controller from the object library onto the storyboard. Then, the book says:
"Now that the view controllers is in place, it’s time to set it up with the correct controller class. Select the view controller and bring up the Identity Inspector. In the Custom Class section of the inspector, change the class to test, and press Return to set it."
My question is: Is it really important to change the class of the view controller to test? Why can't I let the class remain as UIViewController? If I let the class remain as UIViewController, will the outlets and actions created on the view controller in the storyboard not be able to 'communicate' between the View Controller in the storyboard and the test.h and test.m files?
Thanks for reading
Kindest Regards
Yes, it is imperative that you set the class. Fortunately Xcode will not allow you to add outlets in the storyboard if the class set does not define them. If you don't set the correct class then the wrong class will be instantiated at runtime and your custom code will not run.
Keep in mind that the storyboard contains an archive of your design which can be unpacked and used at runtime. It doesn't work automatically and it needs to be configured properly. If, for example, you were to add outlets and actions pointing to an instance of UIViewController instead of test (whose name should start with a capital letter) then you would get an exception at runtime when the archive is unpacked and it is found that the outlets can't be connected, because UIViewController doesn't define them.
YES, it is mandatory. In addition to other answers posted here, I would like to tell you to think over this thing:
Suppose you have more than one Class & ViewController in your demo
project, how do you expect XCode to assign proper Classes to specific
ViewControllers?

What is the reason for storyboards?

New to iOS, just wondering if someone could explain the point of the storyboard.
If i create a view controller and programmatically add to it, what do i need a storyboard for?
Is it only for custom views? custom tables? etc
Thanks.
Long story short, my understanding of storyboard purpose is as follows:
Less code -> less bugs
Visual representation of UI layout simplifies UI creation (views hierarchy, autolayout constraints)
Extremely simple way to set objects' properties (e.g. delegates, gesture recognizers) just by control-drag
From Apple's Document:
Storyboards
Storyboards are the recommended way to design your app’s user interface. Storyboards let you design your entire user interface in one place so that you can see all of your views and view controllers and understand how they work together. An important part of storyboards is the ability to define segues, which are transitions from one view controller to another. These transitions allow you to capture the flow of your user interface in addition to the content. You can define these transitions visually, in Xcode, or initiate them programmatically.
You can use a single storyboard file to store all of your app’s view
controllers and views, or you can use multiple view storyboards to
organize portions of your interface. At build time, Xcode takes the
contents of the storyboard file and divides it into discrete pieces
that can be loaded individually for better performance. Your app never
needs to manipulate these pieces directly. The UIKit framework
provides convenience classes for accessing the contents of a
storyboard from your code.
For more information about using storyboards to design your interface,
see Xcode Overview . For information about how to access storyboards
from your code, see the UIStoryboard Class Reference .

Resources