We will start off by creating a new project and selecting a Window-based Application.
And naming it HelloWorld
Next we want to create a view controller. Go to File > New > New File... and select UIViewController subclass.
A UIViewController subclass works by overriding functions in its base class. The function of particular interest to us is the loadView. This is where we will add controls to our view.
But before we add controles to the view, we need to create a frame which will hold the view.
CGRect frame = CGRectMake(0, 0, 320, 480); creates a frame starting at point (0, 0) (the upper left corner) with a width of 320 pixels by 480 pixels (the size of an iPhone screen). We then allocate and initialize the
view with the frame we just created. Now that we have a view, we can add our label.
Like before, we start by creating a frame to hold the label. Once the frame is created, we can allocate a UILabel, initialize it, set its text, and add it to the view. Lastly, since Objective-C uses reference counting, it is a good idea to release the label to avoid any memory leaks - as a general rule of thumb, if you use a * make sure you release it.
Now that we have a view controller with a label, we need to add the view controller to the window. In your HelloWorldAppDelegate.h start by forward declaring the
HelloWorldViewController class - this will tell the app delegate the HelloWorldViewController class exists. Then create an instance variable of type
HelloWorldViewController, and finally make it a property.
Finally, in HelloWorldAppDelegate.m import the HelloWorldViewController.h file.
Synthesize the viewController instance variable
Then finally, allocate a HelloWorldViewController and add to the window viewController.view as a subview.
Be sure to release viewController when you are done with it!
Build and Go!