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

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?

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.

Unknown class <myClass> in Interface Builder file

i get this error whenever i try to segue from one viewController to another. theres no code and ive been going through google pages and questions on here for hours and i cannot find a solution.
You experience this problem because:
You might have assigned a class to a View Controller in .storyboard file, which does not exist
You might have assigned a class to a View Controller in .storyboard before you created it and thus it failed to automatically set an appropriate Module.
You might have assigned class to a VC, which does not inherit from UIViewController.
Solution: Reassign VC class with help of the dropdown list OR manually chose "Current - Project Name" module from Module dropdown list. Make sure your custom class "extends" (inherits from) UIViewController.

How UIViewController interact with Storyboard under the hood

I'm new to iOS development and this could be a stupid question for some experienced guys...
When I create a new iOS project in Xcode, I get a ViewController class and a storyboard which sets its Custom Class to this ViewController. It looks like there is a "Storyboard" class holding an instance of ViewController, however, I cannot find where this "Storyboard" class is defined.
Even though I know how to create multiple subclasses of UIViewController to handle different views interaction following some tutorials, I still find it uncomfortable to associate these subclasses to the storyboard by selecting them in the storyboard panel. I would rather see something like a "Storyboard" class holding an array of UIViewController.
So my question is, how these UIViewController interact with the Storyboard under the hood?
Thanks
Roughly, it happens as follows:
App launches.
App loads storryboard.
Depending on app's navigation structure, app instantiates each view controller inside the storyboard as needed.
The storyboard contains detailed information on:
Which custom subclass of UIViewController, UINavigtionController, etc. to use for instantiating each view controller in the storyboard.
How to map (connect) each if its view controllers' subviews to the corresponding custom classes' outlets and actions.
But seriously, read Apple's docs. It's all there.
UIViewController has a property named storyboard which refers to the storyboard file associated with the viewcontroller subclass.
Also the view controller code are interecting with the storyboard with connections symboled with #IBOutlet and #IBAction.

Xcode 5 does not recognize custom UIViewController subclass in storyboard

I have a storyboard app in Xcode 5 with many custom view controllers and segues and everything works well, except in one case. In this case, I have a "View Controller Scene" where I am trying to use a custom UIViewController subclass. But even though I specify the subclass, the entry in the sidebar is still called "View Controller Scene" and the View Controller entry for that scene is shown as just the generic "View Controller" (where in every other case, the scene label is changes to reflect the subclass, as is the View Controller entry).
So, Xcode does not appear to believe that I have specified a custom class for the View Controller for this scene. If it were only interface builder that was confused, that would be fine, but when I run the code, when I prepareForSegue: for the appropriate segue, the destinationViewController is a UIViewController, while I need it to be the custom subclass.
I know that my custom class must be a subclass of UIViewController, and it is (although several steps removed -- that is, it is a subclass of a subclass of a subclass of UIViewController).
The problem is not the Xcode 5 bug of not recognizing custom classes (the class name does show up in autocomplete for the Custom Class field) and Xcode has been restarted many times since this custom class was created.
So, what am I missing? What could cause Xcode 5 not to recognize a custom class for a UIViewController?
Happens all the time, easiest way to solve it: quit Xcode and run it again. The view controller should now be visible.
Its a thing that just happens sometimes, I usually ignore it, copy the name in and hit enter, but if its bothering you, you can try to clean and build your project, should start showing up then clean: cmd + shift + k build: cmd + b
good luck!

how do i generate view controller code defined in a storyboard

Here's what i'm doing :
I'm creating an xcode project using storyboards, defining all my screens by drag'n'dropping view controllers and views, but then I need to actually fill those screens with something.
So, i create uiviewcontroller subclasses files in my project, and fill the class name for every view controller in my storyboard. Then, I need to manually drag'n'drop every view elements into my view controller one by one, to generate properties.
I was wondering whether xcode offered the possibility to simply generate all the uiviewcontroller subclasses directly from the storyboard with all the outlets properties already set.
No.You only have to mention outlets and actions for all components which you want to use.If its automatically creates all outlets and properties and you cannot use "code conventions" for your application as you like.Sometimes your labels titles are static and you dont need outlets.so you have to connect each and every component.
http://oleb.net/blog/2011/06/creating-outlets-and-actions-via-drag-and-drop-in-xcode-4/

Resources