This is a tutorial for beginners to the iPhone SDK and includes instructions on programatically creating a Tab Bar Controller with two Tabs and their associated views. As an illustration of how interface elements from one view can interact with those on other views I have placed a UITextField and Button on one view and a UILabel on the other. Anything typed in the text field will be copied to the label when the button is clicked. If you do now want to write all the code for the tutorial you can download it from Github.
Step 1: Read the Hello World tutorial
If you are are unfamiliar with developing in Objective C and iPhone SDK please take a moment to go through the earlier tutorial iPhone SDK Hello World. It also contains instructions on how to install Xcode and the iPhone SDK.
Step 2: Create a project
Select File > New Project or Apple Key + Shift + N to bring up the new project menu. Select the Applications item of the iPhone OS section from the menu on the left, and select Window Based Application from the icons on the right. When prompted enter a project name, I have used TabBar in the sample code. You should have two files TabBarAppDelegate.h and TabBarAppDelegate.m. (See Figure 1)
Step 3: Create the View Controllers
We are going to create two tabs and we need a view controller for each one. Use Ctrl + Nto bring up the new file menu and create two files of type UIViewController SubClass. I named the files View1Controller.m and View2Controller.m (See Figure 2)
Now You should have Six files in your project:
Step 4: Setup the View Controllers
First we will setup the view containing the Button and Text Field. Browse to the View1Controller.h header file and add the following interface elements, UITextField, UIButton, UIView and also add a reference to the Application delegate which will be used to communicate between views. We also create properties for each of the variables we have defined. The final code should look something like this:
In the View1Controller.m file synthesize the properties and write an init method to create the user interface elements. Technically this should be in the load view method but since I am allowing one view to directly access another. WE need them to be initialized even if they are not loaded. The proper way to do this is to store data separately and pull it when the view is loaded but i want to keep this tutorial simple.
We also need to add an action handler to take care of the button click. Add the following method to View1Controller.m. We use the delegate reference to get a reference to the second view controller and the label that we will create in the second view shortly.
On to the second view controller, we do not keep an app delegate instance here because this view does not need to initiate any interaction with the first view. If this were not the case then we would also keep a reference here
In the View2Controller.m file synthesize the properties and write and init method to create the user interface elements.
Step 5: Setting Up the App Delegate
In the TabBarAppDelegate.h header file we define instances of the UITabBarController as well as the two views we have just setup. We create properties for these variables an also declare he relevant classes and import the relevant header files. The final code should look something like this:
In the implementation file TabBarAppDelegate.m update the didFinishLaunchingWithOptions method to create the view controllers and the tab bar controllers and add them to the window. The code for this is as follows.
Step 6: Build and Go
Click the Build an Go button to run your code and you should see the Tab Bar with the first view Loaded. Enter your text and click the button on the screen. Now load the second view by clicking the right Tab and see your text updated there.