How to Use a View Controller Programatically

Matthew Campbell, May 28th

iStock_000004484548XSmall.jpgView Controllers are responsible for managing the graphical elements (the View) that appear on the screen of the iPhone. The graphical elements that make up the view can be created in Interface Builder (IB) or in code.

Some people prefer to use Interface Builder while others prefer to use code. As the statisticians in my last job used to say,

“Some like to see the pictures and some like to read the book”.

Using IB is like seeing the pictures – you see all the controls in front of you and you can lay them out visually. Writing code is like reading the book – you know precisely what is going on with the controls because you can see all the code that they are using.

Pros, Cons & a Confession

I should say right up front that I do not like Interface Builder. It never really made sense to me and I found it difficult to work with. That being said, many people do like IB because it comes with tools that help you lay out your controls in a pleasing way. You can experiment easily with your UI without constantly rebuilding your project to see the results of your code changes.

At any rate, today I am going to show you how to load a view controller with a label in code. I am hoping to use this as an example for the next few posts that will demonstrate different user controls. In the future I am thinking about comparing and constrasting doing these tasks in IB and in code.

Adding a View Controller & Label in Code

Add a New UIViewController Subclass

From your XCode project, select “File”, “New File…” and then “UIViewController Subclass”. This adds a new UIViewController subclass to your XCode project.

Instantiate a New Object Using initWithNibName

Go to your app delegate’s implementation file (this is the file prefixed with your project name with the words “AppDelegate.m” attached to the end. Type this code into the applicationDidFinishLaunching method to instantiate your view controller and add it’s view to your app window. Don’t forget to import your view controller’s interface file at the top of the file.

@end

Add Your View Controller Object to the App Window

@end

Add UI Elements to the View in the View Controller Objects’ loadView Method

To do this step go to your view controller’s implementation file and find the loadView method. Simply add the code here that you need to create your user controls that will appear on the screen. For example, I will add a button to my view controller:

@end

So, that is it. It really only takes three steps to create a view controller and then load its view into your app. In the next few posts I am going to build on this example using different user controls.

My Question to YOU:

Do you prefer to use visual tools like Interface Builder or who you rather use code exclusively? Why or why not?

Learn How To Make Your Own iPhone and iPad iOS apps!

If you are completely new to iPhone programming and want to start to developing iPhone apps this ebook will give you everything you need to get started today.

You Get the 5 Essential Things You Need to Make iPhone Apps

This ebook is much different than the other books out there: it really is a *system* that includes ONLY what you need to know to make iPhone applications. Not only that, but you will get some real insight on how to make a product and how to set up your code in the best way the first time.

Plus, you will get plenty of bonuses including exclusive source code projects. For more information, head on over to the website for the ebook:

Head over to How To Make An iPhone App eBook Website website to find out how to get Matt’s eBook and the bonus source code.

I use both, don’t really have a preference. When I first started programming on the iPhone I tended to not use IB because making the connections was confusing until you get the hang of it. I actually think that doing it in code is faster.

Much prefer Interface Builder and use it the majority of the time. Why have a graphical tool to do the do and then not use it. Obviously there are certain things that can’t be done but I then use IB as the base and then add the required code after.

Thanks for the comments! I am going to try yet again to work with IB and see if it has any benefits. One thing I do like is that it helps out layout the components in a visually pleasing way. It just seems that it is pointless for certain controls like date pickers and table views. (to me).

How to Use a View Controller Programatically…

You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

First I used IB because it seemed easy. Then I realized that complicated things were hard in IB and I went on to doing (and learning) programatic techniques. This went on for a while and helped me learn some solid principles.
Later I became angry, knowing that IB was a good tool and should be making my life easier. So I went back into IB, and everything made sense. I was able to link nibs together to accomplish complex tasks that I thought needed to be done programatically. I actually use nibs for my UITableView Cells, which works surprisingly fast (For text only so far).
My use of IB is almost hybrid now. You can plan to split up your views amongst nibs in certain ways to get flexibility when you need it.
One iPhone complaint I have is the limited control of TableViews in IB. For something so central to the iPhone, it seems like they should be drag drop and go. The design of most TableView data sources us pretty similar and should allow for great control in IB.

I talk about my conversion to IB in one of my blog postings:

http://dannyg.com/iapps/Blog/Entries/2009/4/16_How_I_Grew_to_Love_Interface_Builder.html

@coreyfloyd, @dannyg – thanks for the comments guys. I guess that one of the “problems” I have with IB is that I mostly use table views in my apps. So, not much of a value add for me. But, I am going to give IB another shot starting with my blog posts for next week. I should like it is since I use the similar-ish tool in keynote all the time.

PS: one last vent. – I was just reading through @dannyg’s blog post about learning IB. And it occurs to me that a tool that is meant to simplify things should not require such a lengthly detailed article to explain different ways of essentially coping with the tool. [good post though].

[...] How to Use a View Controller Programatically [...]

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.