Search code examples
objective-ccocoa-touchxcode4ios

MainWindow.xib and ViewController.xib


I don't understand the difference, and with the release of the new xcode 4 (I have the latest 4.4.1) I cant follow some books I bought, I'm lost.

In the book says I need to click on myprogramViewController.xib, but in the new xcode that doesn't appear, do I need to create it?

Also there's missing the MainWindow.xib (but that one I already created it), also in the book says open the myapplicationViewController.m and myapplicationViewController.h to make some changes (I suppose that is the same ViewController.m and myapplicationViewController.m and the same with the .h ones) I'm confused.

So where do I drag buttons and labels and all that stuff? In the MainWindow or in the ViewController? (but do I need to create ViewController?) I'm referring to the .xib one, because I only have ViewController.m and ViewController.h, the .xib one is missing.

I need some help, thank you all!

PS: I'm using single-view application.


Solution

  • When creating a new Single View iOS App from Xcode 4.3+, there isn't any default ViewController.xib. Instead, you have MainStoryboard_*.storyboard which contains a root view controller in which you can add labels, buttons, or any other UI elements.

    This root view controller's identity is ViewController, which refers to the UIViewController subclass defined in ViewController.h/m. When you add a button in the storyboard's root view controller, you can CTRL+drag it to ViewController.h to create an outlet (a connection between an UI element and an instance property) and then write some code in ViewController.m.

    Let's take a look at the following screenshot:

    enter image description here

    I added a Round Rect Button to the root view controller, clicked on "Assistant editor", and CTRL+dragged it to ViewController.h, and named the outlet "button". Then, in the -viewDidLoad method, you can do something like:

    [_button setTitle: @"myTitle" forState: UIControlStateNormal];
    

    Edit

    As far as I know, there is no file owner for a storyboard. Each view controller is instantiated separately with their own UIViewController subclass instance, and you can access each one of them from AppDelegate.m using self.window.rootViewController and its subviews. So the "implicit file owner" is AppDelegate.

    You could follow a storyboard-based tutorial like this one. I personally don't like using storyboards because too many things are implicit. I have one xib per page (view controller) and load them separately from a main root viewcontroller instantiated in appDelegate.

    To see how it works without storyboard, Xcode let you the option not to use them when creating a project. You'll get a simple ViewController.xib/h/m instantiated from AppDelegate.