IPhone Storyboard - Assigning class to view controller in storyboard - iphone

I have created a simple app using storyboard. In storyboard file, i have dragged one view controller. I want to assign a class to the newly added view controller. To do this, I clicked on new file in Xcode, clicked on Objective c class, entered class name MyViewController and subclass is of UIViewController. I went back to storyboard and clicked on Custom class type and given MyViewController in class tab. But it is not taking this. If i unselect the viewcontroller and select, custom class shows "UIView" again.
I am using Xcode version 4.3.3 (4E3002).

xcode does this problem even if everything is fine. Close your xcode project and then quit xcode. Start again and you will be able to find the class in the dropdown menu.

Related

What is the difference between ViewController and UiViewController

I am trying to understand what is the difference between these two concepts.
When I speak about a controller in main.storyboard, do I mean UiViewController or ViewController? are they the same or complementary?
I tried to find some information but got mixed results, I would like you to explain it to me
UIViewController is a class in the UIKit framework. Thing that controls a UIView (aka "view controllers") inherit from UIViewController.
If you create a new iOS app project in Xcode, and choose to use UIKit as the UI framework, it will generate a class called ViewController as part of the project template.
class ViewController: UIViewController {
...
}
This ViewController is a view controller, so it inherits from UIViewController. Note that ViewController is just a random name for the view controller that Xcode chose, it could just as well be YourAppViewController or even FooBarBaz.
You can also find view controllers in the storyboard. Those are the yellow circles with a white square inside. If you have just created a new project, the only one there would be the ViewController Xcode generated. You can see which view controller class a view controller in the storyboard corresponds to by selecting it and checking the "Class" field in the identity inspector:
That iPhone-looking part is the UIView that it controls.

iOS & Swift: ViewController class is defined, but when is it created as an object?

Learning Swift as my first new language in many years, I've come across something I'm curious about using Xcode and creating a new iOS project using the single view template.
In the default ViewController.swift file, UIkit is imported, and then the class ViewController is defined, inheriting from UIViewController. But I can't seem to find out where or how this class is ever created or initialized as an object.
Default ViewController.swift example
If I define my own class, or even want to create another view controller, I must first initialize it as an object somewhere in order to use it. So where is this default ViewController getting made?
Thanks for any help you can offer for me to try to conceptualize this!
Open your "Main.storyboard" file. Make sure View Controller is selected on the left sidebar:
Then on the right sidebar you can see that your ViewController class is in the class field:
So, when your app is loaded, the default storyboard is loaded, and that storyboard is responsible for creating an instance of the ViewController class and set it up for you. To test this out, you could create a new class, say ViewController2 and make it inherit from UIViewController. ViewController2 would then be available in the right sidebar:
And then your ViewController2 code will be used instead of ViewController.
You have changed the location of the ViewControllerQuestion.app file
I believe that the UIApplicationDelegate takes care of the creation of the initial view controller. You still can manually create view controllers as you mentioned and segue to them.
Normally you can create a view controller in a storyboard and use the Interface Builder to set up a segue to that view controller.
I hope this answers your question.

Classes for new View Controller in Storyboard

In my storyboard I drag on a new View Controller. My Storyboard now has two view controllers: the main one that came when I created the file, and the one a dragged on.
When I go into the 'assistant editor' and select the main view controller, I get the ViewController.h class. But when I select the other controller I get UIViewController.h which is an Apple file.
How do I link/create these classes for each View Controller? Is there an automated way to do this, or am I not doing it right.
You need to create your own subclass of UIViewController and set the newly created view controller as the custom class in the storyboard.
Press cmd+n or go to File > New File
Select Objective-C class and hit next
Type UIViewController into the second box and type a name for the new class in the first box (which will be something like MyClassViewController)
Go into your storyboard, select the View Controller you dragged out, look at the inspector and go to the Custom Class Tab and set the custom class to your newly created view controller (e.g. MyClassViewController)

iPhone simulator shows black screen when setting a custom UIViewController class

When creating a single view application in Xcode and dragging a UIButton in, for example, the button shows up when running the application in the iPhone simulator. However, when creating a new UIViewController class and setting that as the custom class of the view controller in the storyboard file, just a black screen is shown in the iPhone Simulator.
What did I do wrong?
I bet the Xcode template included the loadView method in your controller class. You need to delete that as you only override that method when you are creating your interface via code.

How to change the default View Controller that is loaded when app launches?

I have an application, say 'MyApp', which by default loads the view controller 'MyAppViewController' whenever the application launches. Later, I added a new view controller 'NewViewControler' to the project.
I now want the 'NewViewController' to be my default view controller which loads when the app launches.
Please let me know what changes I need to make in my project to achieve this.
Its easy, just:
Open your Storyboard
Click on the View Controller corresponding to the view that you want to be the initial view
Open the Attributes Inspector
Select the "Is Initial View Controller" check box in the View Controller section
Open MainWindow.xib and replace MyAppViewController with NewViewController.
In your app delegate class, replace the property for MyAppViewController with one for NewViewController. Connect NewViewController to its new outlet in Interface Builder.
In application:didFinishLaunchingWithOptions: add NewViewController's view to the window instead of MyAppViewController's view.
Most likely your main NIB file is still set to "MainWindow", check your *-Info.plist file.
If that's the case you can open the MainWindow.xib in Interface Builder. You should see a View Controller item. Bring up the inspector window and change the Class Identity to point to your new class. That should take care of instantiating your class.
As this feels like a "newbie" question (please pardon me if I'm mistaken) I would also highly recommend the following article:
iPhone Programming Fundamentals: Understanding View Controllers
Helped me understand the whole ViewController thing and the IB interaction..
As for me with xcode 4.3.3, all I had to do was simply replace all references of 'MyAppViewController' with 'NewViewController' in the AppDelegate h and m files.
Perhaps all the other steps have been taken out in the newer versions of xcode.
Hope this helps.