Thursday, November 26, 2009

Hello Xcode: add a button to do something

In this exercise, a button is used to trigger a event to happen, to do something.

Start Xcode, start a New Project. In this exercise, select Window-based Application, this template provides a starting point for any application. It provides just an application delegate and a window.



Name it HelloXcode and Save.

Double-click on the MainWindow.xib file to launch Interface Builder.

Drag a Label from Library to Window.


Now, we want to change some attributes. We have to open the Inspector Tool, click Tools from the top menu, and click Inspector.

Click the Label object on the Window pane, the Inspector pane will change to reflect your selection to edit Label attributes.

Make sure the first pane on the top of Inspector is clicked. Change some attributes, such as:
Change Text to Hello.
Change Layout to Center Alignment.
Front Size to 20
...


Next, add a Button, drag a Round Rect Button it from Library to Window. Double click on it, and type the text, Change.


We need a method in our code to do something, when the button is touched.

Now, go back to Xcode. Add the code in HelloXcodeAppDelegate.h

-(IBAction)buttonTouched;



Add the code in HelloXcodeAppDelegate.m

-(IBAction)buttonTouched{
UILabel *label = (UILabel*)[window viewWithTag:10];
label.text = @"Hello Xcode";
}



Switch back to Interface Builder, select the Label, and select the first pane on Inspector. Enter 10 on the Tag field.


Lastly, we need a connection between the button touch event to the method buttonTouched.

Click to select the button, click to select the second pane of Inspector, drag the circle beside Touch Down event to the Hello Xcode App Delegate object in the Document window.


When you release mouse, Interface Builder show you the available method can be connect, right now only one method available, select it.




Now you can Build and Go the application.

Tuesday, November 10, 2009

Add TextView using Interface Builder

Interface Builder is a visual design tool you use to create the user interfaces of your iPhone OS and Mac OS X applications. Using the graphical environment of Interface Builder, you assemble windows, views, controls, menus, and other elements from a library of configurable objects. You arrange these items, set their attributes, establish connections between them, and then save them in a special type of resource file, called a nib file. (The term “nib” is historical and is an acronym for “NextSTEP Interface Builder.“) A nib file stores your objects, including their configuration and layout information, in a format that at runtime can be used to recreate the actual objects

Expand Resources in Groups & Files on the left, double click HelloWorldViewController.xib to open it in Interface Builder.


There should be three windows opened similar to that shown below:

(If you can't see the window of Library, it can be opened by Tools->Library from Top Menu of Interface Builder.)

Here, I want to place a text on the screen only:
Scroll down from Library, until you see the object TextView, drag it into View window. It can be edited, moved and resized... Change the text to "Hello World".


Save it.

Switch back to Xcode and click Build and Go button.


So simple!

Note: Interface Builder generate XML only, it will not generate any functional code.

Sunday, November 8, 2009

A dummy "Hello World", my first xcode project using iPhone SDK 3

Start Xcode
By default, it locate in /Developer/Applications/
(close the Welcome Window now.)

Create a New Project
Click File->New Project from the top menu.


- Select Application under iPhone OS heading on the left, and select View-based Application template on the right. This template provides a starting point for an application that uses a single view. It provides a view controller to manager the view, and a nib file that contains the view.


Click Choose... to choose the location to save the works, and the name to save as.


Xcode will create the new project.


Click the Build and Go button on the top right now, to launch the iPhone Simulator.


My first iPhone program, with nothing, is here.