You are on page 1of 48

Bi 1: Gi i thi u cc cng c

l p trnh iPhone

Theo kinh nghi m c a ti th nh ng ng i qu quen v i h i u hnh windows v lm vi c trn n n windows th r t kh m thay i thi quen s d ng m t h i u hnh khc. Tuy nhin, c ng theo kinh nghi m c a ti th ch c n b m t th i gian ng n ch u kh lm quen v i ci m i th t h n s th y c nhi u i u th v m bn windows khng c. L y m t v d i n hnh nh khi b n chuy n t windows sang MAC OS b n s th y MAC ch ng c g hay ho c , kh dng, giao di n th ko c bng b y nh windows 7, khi c n ng d ng cho MAC th tm l l i c ng ch ng ra, m c ra th c ng kh tm c b n crack. T t nhin l c ng ph i tu vo tnh ch t cng vi c m ng i ta nn ch n h i u hnh no cho ph h p. y, cng vi c c a chng ta l l p trnh ng d ng trn iPhone nn MAC OS l m t trong s cc y u t m b n ph i c. Ngoi ra, l p trnh c ng d ng iPhone b n c n c 1 b iPhone SDK do Apple cung c p, phin b n m i nh t hi n nay l 3.1.2 v m t b XCode cng phin b n dng coding v ko th giao di n. iPhone SDK v XCode th c th d dng download t i trang web c a Apple cn MAC OS th t t nh t l mua a dnh cho PC. Vi c ci t MAC OS trn PC th c s khng ph i d dng cht no v theo nh kinh nghi m ci MAC c a ti th m i my PC c m t dng MAC OS ring c hack c th lm vi c c trn n n ph n c ng t ng ng. Do m khi b n ch n mua a MAC nn ch n c u hnh ph n c ng c a my. Ch ng h n nh card m thanh l do realtek s n xu t hay hng khc, card ho VGA hay Nvidia, card m ng,... Ch c n b n ch n c u hnh ph n c ng l ch m t cht trong qu trnh ci t th coi nh cng c c. Ngn ng l p trnh ng d ng trn iPhone l Objective-C. y l m t ngn ng l p trnh h ng i t ng. Tc gi pht tri n k th a trnh bin d ch C v t o nn trnh bin d ch m i cho Objective-C. V v y m trnh bin d ch Objective-C c th bin d ch c c ngn ng C/C++. Trong l p trnh iPhone b n c th vi t c 3 ngn ng trong cng m t application. V XCode th ph i ni l qu tuy t v i khi l p trnh trn iPhone do n h tr r t m nh cho iPhone. B n s ko c n ph i quan tm v cc i t ng UI n a. V Xcode qu n l cc i t ng UI r t ch t ch . Ch ng h n nh thay v b n khai bo i t ng UIButton th b n ch vi c ko i t ng button vo View v nh x n t i bi n UIButton trong m ngu n n s t ng kh i t o khi load ln... C l ch m t vi l i c ng kh m di n t h t th tr i nghi m v c m nh n. Bi ti p theo chng ta s h c v cc trc c a chng. it c ci hay c a XCode. Thay vo b n ch c

ng UI trong l p trnh ng d ng trn iPhone v c u

(Xin l u , cc ti li u v iPhone hon ton c copy t trang iCodeblog.com V m t vi ng i hi u nh m ngh mnh copy b n quy n. Nn edit thm ch ny m i ng i bi t r. Ngoi ra, theo mnh ngh th th y ti li u hay ho c c ki n th c g mnh cho l b ch th mnh s post ln v c g ng nhn r ng ra m i ng i d tm ki m h n khi s d ng google search)

Bi 2: UITableView Hello world


Friday, January 22, 2010 1:57:00 PM iPhone Trong bi ny s h ng d n cho cc b n cch t o m t ng d ng "Hello world" s d ng UITableView. M c tiu c a bi h c: - Create a New Navigation-Based Application - Learn About the Default Files - Update the UITableView Cells to Display Hello World Text Creating a New Navigation-Based Application Start a new iPhone OS Project Click Xcode > New Project and a window should pop up like this:

Make sure Application is selected under iPhone OS and then select Navigation-Based Application. Click Choose It will ask you to name your project. Type in Hello World and lets get started.

Learn About the Default Files

What is all this stuff? There are quite a few files that get added to your project. At first glance, this looks kind of intimidating. Dont worry, we only need to edit one of them. Here is a quick explanation of the different files. You dont have to read this part but having this many files is what confused me the most when I started developing for the iPhone. 1. CoreGraphics.framework, Foundation.framwork, UIKit.framework You guessed it, this is a set of library functions provided by Apple that we use in our application. We use these as includes similar to any other language that includes library functions. 2. HelloWorld.app This is your app that gets installed on the iPhone. We dont really need to worry about this right now 3. Hello_World_Prefix.pch This is another include file that gets compiled separately from your other files so you dont need to include it on each file. It contains some code to include the data inside the frameworks. 4. Hello_WorldAppDelegate.h This is a header file that contains all of our definitions for variables that we will be using. Its very similar to a header file in C or C++; 5. Hello_WorldAppDelegate.m All of the magic starts here. Consider this file our starting point for execution. The main.m file invokes this object. 6. Info.plist This contains various meta information about your program. You wont really need to edit this until you are ready to start testing on the iPhone 7. main.m Like most programming language, this file contains our main function. This is where execution begins. The main function basically instantiates our object and starts the program. You shouldnt need to edit this file. 8. MainWindow.xib This contains the visual information of our main window. If you double click on it, it will open in a program called Interface Builder. We will get to this a little later. Just on thing to note is this file does not contain any code. 9. RootViewController.h, RootViewController.m - These are files for a view controller that gets added to our main window. Basically, Apple has already created a simple interface when you clicked on Navigation-Based Application. Since most navigationbased applications use a Table View, Apple has provided it for us to use. 10. RootViewController.xib This is a view that Apple has provided that emulates a table. It has rows and columns. We will be displaying our Hello World text inside one of these rows Now, all of these files together create a basic program. Go ahead and click on the Build and Go button at the top of Xcode. Make sure the drop-down on the top left says Simulator | Debug, this tells Xcode that we are testing on the iPhone simulator.

You will see the iPhone simulator start and your program will launch. Its not very interesting at the moment. All it shows is the Table View that Apple has added for us. So what we are going to do is add a row to this table view.

Update the UITableView Cells to Display Hello World Text


Lets write some code Start by opening RootViewController.m. This is the view controller that Apple added to our main view. All of the functions you see already created in here are functions that have been overridden from the Table View super class. Since we are editing a table, all of these functions will be related to editing a table. So find the function called numberOfRowsInSection.

This function tells the application how many rows are in our table. Currently, it returns 0. Lets change that to return 1. This will tell the application that we want 1 row in our table. Now go down to the function called cellForRowAtIndexPath. This function gets called once for every row. This is where we define what content to display in a given row. In this case we want the row to be a string that says Hello World.

What this function is doing is creating a new cell object and returning it. The code between the i(cell==null) block checks to see if we have created a cell before, if not build a new cell, otherwise use the one we created before. This helps in performance so we dont need to build new cells each time we visit this function. So right before the // Set up the cell comment add the following code: [cell setText:@"Hello World"];

We are basically calling the setText method of the cell object and pass in the string Hello World. As you should know from reading Apples Objective-C overview, strings begin with an @ symbol. Thats it! Click the Build and Go button again to launch the iPhone simulator. You should now see a screen like this:

You can now use this code as a base for creating a Navigation Based application. In a later tutorial, I will detail how to populate this table view with an array of strings to create navigation for a simple program. If you get lost at any time, you can download the sample code for this program here hello-world. I hope you enjoyed the tutorial and if you have any questions, feel free to leave them in the comments. Happy Xcoding!

Bi 3: Beginner Interface Builder Hello World


Friday, January 22, 2010 2:05:21 PM iPhone Bi ny h ng d n cch xy d ng m t ng d ng "Hello world" b ng cch ko th trong interface builder c a XCode. Trong bi ny cc b n s ch cv : - Create a New View Based Project (T o m t project v i i t ng c kh i t o ban u l UIView) - Opening the iPhone Simulator

- Adding UI Elements to your home screen - Executing the code (Cch th c thi ch ng trnh) In my last tutorial UITableView Hello World I said that there are many ways to write a Hello World tutorial for the iPhone. Here is one using Interface Builder. Last time, I demonstrated a simple way to populate one cell in a UITableView with some text. Todays tutorial is even simpler. I will show you how to work with Interface Builder to create a simple layout for you application. In fact, you wont write any code at all! In my next tutorial, I will detail how to interface with your UI components in code. In this tutorial you will learn:
y y y y

Create a New View Based Project Opening the iPhone Simulator Adding UI Elements to your home screen Executing the code

Create a New View Based Project


Lets start by opening up XCode and Creating a new View-Based Application. Do this by clicking on File > New Project. Make sure that Application is highlighted under iPhone OS and select View-Based Application. Name this project HelloWorldViews.

So at this point, Apple has added some default files to get us started. They have added the main window for us to edit along with the code to launch the window. Now in iPhone terms, each window is called a View. Since you can only see one view at a time, you will need to create a new view for each screen in your application. In this tutorial, we will be sticking to editing the view Apple has provided us. In later tutorials, I will go into how to add new views and transition between them. Go ahead and click Build and Go.

Opening the iPhone Simulator


The program should compile and launch the iPhone Simululator. Your screen should look something like this.

Advertisement <script type="text/javascript"> //<![CDATA[ ord = window.ord || Math.floor(Math.random()*1E16); document.write('<script type="text/javascript"

src="http://ad4.netshelter.net/adj/ns.icodeblog/general;kw=;tile=4;sz=300x250,336x280;ord=' + ord + '?"><\/script>'); //]]> </script> Not very interesting right? Excuse my crappy screenshot. So lets add some UI components to our view to make it more interesting. To do this we will be editing HelloWorldViewsViewController.xib . A file with the .xib extension is known as a nib file. These files open with Interface Builder and are where you define the look and feel of your application. Apple is nice enough to have provided us with one.

Adding UI Elements to You Home Screen


Once you open Interface Builder, you should see a few different windows You should see a blank window that has the same dimentions as the iPhone that says View up in the top. If not click the View icon in the smaller box. This is the main window we will be editing. To the right, you should see a tool box that contains many different UI components. Most of them should look familiar if you have ever used an iPhone application. So lets add a few of them to our view. Just click on the ones you want and drag them on to your view. Make sure you at least use a Label. For this tutorial, I have used a Label, Search Bar, and a Tab Bar. Notice when you drag the search bar and tab bar onto the view, they size correctly to the screen. Also, if you move the items around, you will see blue lines that guide you. These are in place to help you line up components and center them. After you have added the label, click on it. Now lets open the Attributes Inspector to change the text of the label. To do this click Tools > Attributes Inspector. At the top of the Attributes Inspector you should see a box labeled Text. If you dont click on the label again to select it. Put whatever you would like the label to say in that box. I put Hello World. You can update other properties of the label including color and font using the Attributes Inspector. You may need to resize the Label to accommodate your new text. To do this click on the label and you will see little boxes around it that you can drag to resize the text area. After you are done, your interface should look something like this:

Now press Apple-S to save this file. We are done with Interface Builder so you can close it. Your application should now be ready to execute.

Executing the Code


Go ahead and click Build and Go again to launch the iPhone Simulator. Your screens should look something like this:

There you have it! A simple iPhone application without writing a line of code. If you click in the search box, the iPhone will automatically pull up the keyboard. Thank you for reading this tutorial and I hope that you have learned something. In my next tutorial, I will show you how to use code to interface with our UI components. If you have any questions, feel free to leave them in the comments. Happy iCoding!

Bi 4: Connecting Code to An Interface Builder View


Friday, January 22, 2010 2:19:12 PM iPhone Finally, we get to write some real code! In this tutorial, I will show you how to create an interface using Interface Builder and connect it to your code. We will be creating a UITextField, UILabel, and a Button. Now, dont be intimidated that this tutorial is so long. I have really went into detail explaining everything as I go. You could easily scan over it and get the gist of it. Heres how the application will work:

1. 2. 3. 4. 5.

The user will tap inside a text box which brings up the iPhones keyboard The user will type their name using the keyboard The user will press a button The label will update with a greeting containing that users name (ex. Hello Brandon!) If the user fails to enter in text, the label will say something like Please Enter Your Name

Prerequisites: This tutorial assumes that you have completed the following tutorials
y y

Hello World Tutorial Using UITableView Beginner Interface Builder

In this tutorial you will learn:


y y y y y

Create a new View-Based application Create a simple user interface Write code to connect to an interface Connect the interface to the code Update the interface based on user interaction

Like the last tutorial I wrote, we are going to need only one view. So we will just use Apples View-Based Application template. So click File -> New Project. Select View-Based Application and name it ViewBased (You can name it whatever you want).

So like last time, Apple has provided us with most of the code needed to get this app up and running. You can click Build and Go to launch the simulator, but all you will see is blankness. Lets get started by double clicking on ViewBasedViewController.xib. This is a nib file that opens with Interface Builder. It contains information regarding the layout and controls of our view. Once you do this, Interface Builder should open up. It will look something like the screenshot below.

A few notes about interface builder Library The right-most window contains all of your controls that you can add to your view. For this tutorial we will be using a TextField, Label, and Button. The next window to the left of that contains objects that we will connect our interface to. View represents the view of this nib file (basically the interface). Files Owner is the object that links the interface to the code. View - This is your user interface for your iPhone application. This window is where you will drop controls from the right-most window. Attributes This is where we will set the styling properties of our controls

Add a Text Field


The first thing you want to do is drag a Text Field from the library box on to your view window. You will see some blue lines to guide you. These are in place to help line up controls as well as center them. Once you have added the Text Field to the View, move it around until its in a position that you are happy with. Next, stretch each side of the text box so that it spans accross almost the entire view area. (The blue lines on the right and left will let you know when to stop.) Now we are going to set some of the attributes of the Text Field. If the Attributes Inspector doesnt appear, click on the Text Field and then click Tools -> Attributes Inspector.

y y y y

In the Placeholder field type in Name. This is the default text that appears in the Text Field before a user types anything. For Capitalize select Words This tells XCode that we want to capitalize each word For Return Key Select Done. This makes the return key on the keyboard say Done rather than return. Also, make sure Clear When Edit Begins is checked

Add a Label
Drag a Label from the Library onto your view. Similarly, stretch it the length of your view and place it where you would like. Lets change the default text of the label. If the Attributes Inspector doesnt appear, click on the Label and then click Tools -> Attributes Inspector. In the Text field type in Enter your name above (or below depending on where you chose to put the label in relation to the Text Field.

Add a Button
Now drag a Button from the library onto your view. Stretch it and position it however you would like. Now we need to add some text to the Button. Click on the button and then click Tools -> Attributes Inspector. In the Title field, type Display.

We are now done creating our interface. It should look something like this:

Advertisement <script type="text/javascript"> //<![CDATA[ ord = window.ord || Math.floor(Math.random()*1E16); document.write('<script type="text/javascript" src="http://ad4.netshelter.net/adj/ns.icodeblog/general;kw=;tile=4;sz=300x250,336x280;ord=' + ord + '?"><\/script>'); //]]> </script> Lets return to our code Close Interface Builder and go back to Xcode. The files that link an interface to some code are called View Controllers. Lets open up ViewBasedViewController.h. This is the file where we will declare all of our interface variables. Add the following code to you ViewBasedViewController.h.

Interface Builder uses IBOutlets and IBActions to connect to the code. Here is a brief explanation of each line of code.
y y

IBOutlet UITextField *textName creates an outlet to connect to the text field we created in interface builder. We use this variable to get information from the text field. IBOutlet UILabel *lblHello An outlet that connects our label on our interface to the code. This variable is used to set the value of the label.

Now that we have declared these variables, we need to make them properties. This allows us to set certain attributes that are associated with the variables. Retain tells the compiler that we will handle the memory management of this object (dont forget to release it when you are done). Otherwise it will get cleaned after being instantiated. There is one other thing here. - (IBAction) updateText:(id) sender; This is the function that will get called when the user presses the button that was created in Interface Builder. We are simply declaring it here in the header file. We will implement this function a little later in the tutorial. Now, we need to connect the interface to the code. Double click on ViewBasedViewController.xib again to open up Interface Builder. 1. Connect the View 1. Click anywhere on the background of your view (anywhere that is not the Text Field, Label, or Button). Now click Tools -> Connections Inspector. Next to New Referencing Outlet, you will see a circle. Click in that circle and drag the blue line to the Files Owner object and release it. The word view should pop up. Click on it. You have now connected the view to your proxy object. You should now see:

2. 2. Connect the Text Field 1. Click on the Text Field in your View window to select it. Then click Tools -> Connections Inspector. You will now see a circle next to New Referencing Outlet. Click in that circle and drag it over to the Files Owner object. A message will pop up with txtName. Click on txtName and the connection is made. You should now see:

2. 3. Connect the Label 1. Click on the Label in your View window to select it. Then click Tools -> Connections Inspector. You will now see a circle next to New Referencing Outlet. Click in that circle and drag it over to the Files Owner object. A message will pop up with lblHello. Click on lblHello and the connection is made. You should now see:

2. 4. Connect the Button 1. Click on the Button in your View window to select it. Then click Tools -> Connections Inspector. You will now see a circle next to Touch Up Inside. This is the trigger that gets called when a user presses the button. Click in that circle and drag it over to the Files Owner object. A message will pop up with updateText. Click on updateText and the connection is made. You should now see:

2. Now all of the connections should be set up. Go ahead and close Interface Builder. We are done using it. Open up the file ViewBasedViewController.m . This is the file where we will implement the updateText function. Add the following code

This code is pretty straight forward and easy to follow. I will explain it line-by-line: @synthesize txtName,lblHello; Most of the time when creating (private) variables, you need to specify what are called getter and setter methods. Theses functions get the value of a variable and set the value of a variable. What synthesize does is creates these methods for you under the hood. Pretty handy Next we will implement our updateText method. I started by creating a temporary string. This is the string that we will insert into the text of the label. The next line checks to see if the user has entered any text int the Text Field. txtName.text returns an NSString. We are simply calling the length method on a string. If the length is 0, then obviously the user has not entered any text. If this is the case, we set the temporary string to Please enter your name: instructing the user to enter in their name. If the user has entered in some text, a new string is allocated. The initWithFormat method is similar to printf in C. So, I used the string Hello %@!. The %@ in the string means we will be substituting it for a string. To get the value of the Text Field we again use the txtName.text property. Finally, the value of the Label is set by calling lblHello.text. This calls the text property of label and sets the text to our temporary string variable. The last line ; releases the temporary text field from memory. This is necessary to write an efficient iPhone application. If you want to know why a lot of iPhone apps crash, its because the developers dont follow through with this step.

Thats it! Click Build and Go. The application should launch in the iPhone Simulator. When you click inside the Text Field it should bring up the iPhones keyboard (you can also type with your keyboard). Enter in your name and click Display. Here are a few screens of what your app should look like.

User Clicks Display without typing in their name

User types in their name and clicks Display

Well, that concludes this tutorial. I hope that I was able to help you out on your road to iPhone glory. If you get lost at any point you can download the code to this tutorial here ViewBasedSampleCode. As always, if you have any questions or comments, be sure to leave them in the comments section of this page. Happy iCoding!

Bi 5: Transitioning Between Views


Monday, January 25, 2010 3:40:15 PM iPhone Ph n ny s h ng d n cho cc b n cch chuy n qua l i gi a cc UIView v i nhau. C 2 cch chuy n qua l i gi a cc View: 1. N u s d ng NavigationBar th n gi n l push view c n chuy n vo NavigationBar v th c t NavigationBar l m t stack ch a cc View. i t ng View c hi n th l i t ng trn nh stack. Ng c l i quay l i View tr c th ch c n pop View trn cng ra. Cch lm chi ti t nh th no th bi ny s t ng b c h ng d n c th cho cc b n. 2. N u khng s d ng NavigationBar v b n mu n chuy n t m t View hi n t i sang m t View khc th b n thn i t ng View cung c p cho chng ta hm addSubView. N u b n ang ng m t i t ng k th a t UIView th s d ng l nh [self addSubView:<View Object need to switch>] D i y l nguyn b n tutorial trong trang http://www.icodeblog.com This tutorial will focus on transitioning from one view to another. We will be utilizing Apples UINavigationController. I will be using the code from the Hello World tutorial that I previously wrote. So if you have not completed it yet, go ahead and do it and come back to this page. (Its quick I promise). You can view it here.

In this tutorial you will learn:


y y y y y

Add A New View Add A View Controller Set Up The Transition To The View Connect The View To The Code Add A Back Button

The first thing we are going to do is change our Hello World text to say something that sounds more like navigation. Go ahead and open RootViewController.m. Location the cellForRowAtIndexPath method (its the one that you edited to display Hello World in the table cell. Change the line: [cell setText:@"Hello World"] ; to [cell setText:@"Next View"];

Add A New View

We will now add the view that we will be transitioning to. Click on RootViewController.xib and this should open up Interface Builder. We dont actually need to edit this file. Once inside Interface Builder click on File -> New and select View.

It will add a blank View to your project. For now, we will keep it simple. Go ahead and drag a new Label on to the View. Double click on the label and change the text to whatever you want. I set mine to View 2 (I know, not very imaginative).

Lets save the view. Click File -> Save. Call it View2. Make sure that you save it inside your Hello World projects directory. It may want to save it somewhere else by default.

Next, you will see a screen asking you if you want to add the View to your project. Check the box next to Hello World and click Add.

Close Interface Builder. Drag the View2.xib file into the Resources folder, if it didnt appear there by default (this will help maintain organization).

Add A View Controller

Now we need to create a ViewController class. This class will be used to connect the view that we just created to our code. Inside of Xcode click File -> New File Select UIViewController subclass and click Next.

Name it View2ViewController and make sure Also create View2ViewController.h is checked. Click Finish to continue. This will add the new ViewController to your project.

For organization sake, drag your View2ViewController.h and .m files into the Classes folder if they didnt appear there to begin with.

Set Up The Transition To The New View


Open up RootViewController.h and add the following code:

This code should be pretty self explanatory, but I will explain it anyway. The import statement #import View2ViewController.h gets the header file of the ViewController that we created and allows us to create new instances of it. Next, we declare a variable called view2ViewController which is of the type View2ViewController. One thing that I want to point out is the first part starts with a capitol V which is the type of object that we are declaring. The second part starts with a lower case v. This is our variable name that we will use to reference our ViewController object. Finally, we make our variable a property to set additional information.

Now, open up RootViewController.m and add the following code underneath @implementation RootViewController. This creates default getter and setter methods for our view2ViewController property.

Advertisement <script type="text/javascript"> //<![CDATA[ ord = window.ord || Math.floor(Math.random()*1E16); document.write('<script type="text/javascript" src="http://ad4.netshelter.net/adj/ns.icodeblog/general;kw=;tile=4;sz=300x250,336x280;ord=' + ord + '?"><\/script>'); //]]> </script> Next find the function didSelectRowAtIndexPath. It may be commented out. If it is, go ahead and uncomment it. This method gets called (automatically) every time a table cell gets tapped. Notice that it takes an indexPath. This will be useful later on when I show you how to populate a UITableView with an NSArray. We will ignore it for now. Add the following code to the method.

First we check to see if self.view2ViewController is null. This will happen the first time the user presses on the table cell. After this, the viewController gets stored in memory to optimize performance. Next we create a new instance of a View2ViewController and set it to our view2ViewController. Remember to pay attention to a capitol V verses a lowercase v. After we set this viewController to our viewController, it should be released. Remember, objective-c does not have a garbage collector, so we need to clear all of our unused objects from memory. Finally, the last line of code is what actually transitions our view to the newly created view. The navigationController object is a stack that contains viewControllers. The view at the top of the stack is the one that gets rendered. So all we are doing is pushing a viewController onto this stack. There last part animated:YES, tells the compiler that we want an animated transition to the next view.

Connect The View To Code


Before this code will execute, we must connect the code that we just wrote to the view we just created. Double click on View2.xib to open it up in Interface Builder. We need to associate our

View2ViewController class object with the view so click on the Files Owner object and then click Tools -> Identity Inspector.

Click the dropdown next to class and select View2ViewController.

Next click anywhere on your view to select it. Click Tools -> Connections Inspector. Click in the circle next to New Referencing Outlet, drag it to the Files Owner object and release it. The word view will popup. Go ahead and click on it.

Close Interface Builder. You can now click Build and Go. When you click on the words Next View, you will see the screen transition to your new view. There is still one thing missing. There is no back button in the NavigationController at the top. Apple actually adds this for us, but we need to set a title on our main view.

Adding The Back Button

Close the iPhone Simulator and open RootViewController.m. In the viewDidLoad method (gets called when the view is first loaded) add the following code.

Since RootViewController extends Apples class UITableViewController, it comes with a title property. You can set this to anything you want. I have set it to the string Hello. Now click Build and Go and you are done. Here are a few screenshots.

When you click on Next View it should transition to:

Notice the back button at the top with the text Hello. If you press it, your view will be popped from the NavigationController stack and the previous view will be shown. If you have any problems/questions/comments post them in the comments. Im pretty good about answering them as it emails me when you do so and I receive them on my iPhone. If you have any problems, you can download the source code here Hello World Views Source. Happy iCoding!

B n s thao tc v i b cng c Xcode th c hi n m i cng vi c trong qu trnh pht tri n cc ng d ng c a mnh. T o new iPhone OS Project Vo Xcode>New Project m t c a s m i s xu t hi n nh hnh d i

B n ph i ch n Application bn d i iPhone OS v ch n Navigation-Based Application. B m Choose v b n s c yu c u t tn cho project. G vo HelloWorld v ta cng b t u. Tm hi u v nh ng files m c nh B n c t h i nh ng file g m nhi u v y? C kh nhi u file ct ng thm vo project c a b n. L n u m i nhn, mnh c c m gic nh l ang b e d a. ng qu lo l ng, chng ta ch c n ch nh s a m t file trong s . Sau y mnh s gi i thch s qua v cc file khc. B n c th b qua khng c n c ph n ny, tuy nhin l n u tin khi th y nhi u file nh v y khi l p trnh iPhone, mnh c m th y kh b i r i. 1. CoreGraphics.framework, Foundation.framework, UIKit.framework- Ch c b n on c, y l mt t p h p cc library functions cung c p s n b i Apple m chng ta s s d ng trong cc ng d ng c a mnh. Chng ta s d ng chng gi ng nh vi c include th vi n trong cc ngn ng l p trnh khc. 2. HelloWorld.app y chnh l ng d ng c a b n c ci t vo iPhone. Ngay lc ny, chng ta ch a th t s c n quan tm n n. 3. Hello_World_Prefix.pch y l m t file c include khc, bin d ch t nhi u file ring bi t gip b n khng c n ph i include t ng file m t vo trong project. File ny ch a m t s o n m

include d li u bn trong frameworks. 4. Hello_WorldAppDelegate.h y l file header ch a t t c cc nh ngh a v bi n m ta s s d ng. T ng t nh m t file header trong C hay C++. 5. Hello_WorldAppDelegate.m Ph n c t li c a ng d ng n m y. File chnh l i m b t u c a ng d ng. File main.m g i object ny. 6. Info.plist file ny ch a nhi u thng tin(meta information) khc nhau trong ch ng trnh c a b n. B n s khng c n ph i ng ch m n file ny cho n khi b n s n sng cho vi c test th ch ng trnh trn iPhone. 7. main.m Gi ng nh h u h t cc ngn ng l p trnh khc, file ny ch a hm main. y l kh i i m c a ch ng trnh. Hm main v c b n kh i t o object v kh i ng ch ng trnh. B n khng ph i ch nh s a file ny. 8. MainWindow.xib Ch a cc thng tin tr c quan v c a s mn hnh chnh(main window). N u b n nhy p ln n, m t ch ng trnh c tn g i Interface Builder s c cm ra.Chng ta s ni n Interface Builder sau. B n c n nh m t i u, file ny khng ch a b t k m t dng m no c . 9. RootViewController.h, RootViewController.m Nh ng file ny dng cho m t view controller c thm s n vo c a s mn hnh chnh(main window). V c b n, Apple t o s n m t giao di n n gi n ngay khi b n b m vo Navigation-Based Application. V h u h t cc ng d ng ki u navigation-based u s d ng m t TableView, Apple cung c p s n cho chng ta. 10. RootViewController.xib y l m t view m Apple cung c p s n d i d ng table. N c cc dng v c t. Chng ta s hi n th l i cho HelloWorld trn m t trong s cc dng c a table ny. T t c cc file trn t p h p l i t o nn m t ch ng trnh c b n. No, b n hy b m vo nt Build and Go pha trn c a Xcode. Ch ci drop-down pha trn bn tri hi n th Simulator|Debug, c ngh a l b n b o Xcode r ng b n ang test trn thi t b gi l p iPhone(iPhone Simulator). C p nh t UITableView Cells hi n th l i cho HelloWorld. By gi chng ta s code t cht Ta b t u b ng vi c m file RootViewController.m. y l view controller m Apple thm s n vo c a s chnh c a ch ng trnh. T t c cc hm m b n th y ct os n y c overridden t l p cha TableView. V chng ta ang ch nh s a table nn cc hm ny lin quan n thao tc ch nh s a table. Tm hm numberOfRowsInSection. Code:
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{ return 0; }

Hm ny cho ng d ng bi t table s ch a bao nhiu dng. Hi n t i n tr v 0. Chng ta s s a l i thnh return 1. Ta mu n ng d ng s hi n th 1 dng trn table. By gi , ko xu ng hm cellForRowAtIndexPath. Hm ny c g i m t l n i v i m i dng. y l n i ta s nh ngh a nh ng g s c hi n th t i t ng dng. Trong tr ng h p ny ta mu n hi n th l i cho Hello World. Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @MyIdentifier; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; } // Set up the cell return cell; }

Nh ng g m hm ny ang lm l t o ra m t i t ng cell v return i t trong kh i if(cell==nil) ki m tra xem chng ta t o ra m t cell tr c ch m t cell m i, cn khng th s d ng cell t o tr c . Vi c ny gip cho performance t t h n v ta khng ph i t o ra cell m i m i l n ch ng trnh g thm dng code sau vo ngay tr c dng ch thch // Set up the cell: Code:
[cell setText:@Hello World]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @MyIdentifier; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; } [cell setText:@"Hello World"]; // Set up the cell return cell; }

ng ny. o n m a. N u ch a, t o ng d ng c i l i hm ny. B n

Ta g i ph ng th c setText c a i t ng cell v truy n vo chu i Hello World. B n bi t r ng trong Objective-C, chu i b t u v i k t @. By gi b m nt Build and Go kh i ng iPhone Simulator. B n s th y m t mn hnh nh d i y:

L p trnh iphone IPHONE - S N PH M LM NN CU V C I N THO I THNG MINH IPhone l g ? -

C CCH M NG TRONG L NH

IPhone l m t i n tho i di ng tch h p v mynghe nh c a ph ng ti n c pht b i Apple.Thi t b ny k t h p cc ch c n ng chnh c a m ts ti n ch ph bi n i n t trong m t thi t b duynh t, v i m t giao di n c chia s v h i nh pgi a chng. Ngoi ch c n ng chnh c a n nh lm t i n tho i di ng, iPhone cn bao g m mynghe nh c, xem video, my nh k thu t s , PDA,v l m t b truy n thng Internet. L ch s pht tri n c a Iphone : IPhone u tin c cng b b i c u Gim c i u h n h S t e v e J o b s c a A p p l e v o n g y 09/01/2007 v pht hnh vo n g y 2 9 / 0 6 / 2 0 0 7 . IPhone th h th 5, iPhone 4S, c cng b vongy 04/10/2011, v pht hnh vo ngy 14/10/2011, hai ngy sau khi pht hnh iOS 5.0, h i u hnh c a Apple cho cc thi t b c m tay . i u g t o nn s khc bi t c a Iphone ? 1.Mn hnh c m ng r ng V i Iphone , mn hnh chnh l i n tho i . M i th c th c hi n thng qua mn hnhc m ng 3.5 inch v ngy cng l n h n qua cc phin b n ti p theo c a IPhone .2 . C m ng a i m

Iphone chnh l i n tho i thng minh u tin s h u ch c n ng c m ng a i m . Ta cth phng to m t trang web , m t b c nh ho c m t t m b n b ng cch s d ng ccngn tay .3 . S e n s o r s ( B c m b i n ) C m bi n th nh t l gia t c k , khi b n quay ngang i n tho i , n s t ng thay i mnhnh hi n th theo ch phong c nh. Tnh n ng ny l v cng h u ch xem nh tonc nh, video ho c l t qua cc album c a cc b n C m bi n th hai pht hi n nh sng xung quanh v i u ch nh sng c a mn hnh phh p ti t ki m n ng l ng. -

C m bi n th ba s v hi u ha mn hnh khi b n h ng i n tho i v pha khun m t c a b n, v v y b n s khng c quay s trong khi ni chuy n trn thi t b .4 . I p h o n e O S P l a t f o r m ( i O S ) IPhone s d ng h i u hnh c ngu n g c t Mac OS .I. Cc l p v n n t ng c a iOS 1. T ng quan v iOS. iOS (tr c y c g i l iPhone OS) l h i u hnh di ng c a Apple. u tin OS ch c pht tri n cho iPhone, sau ny c m r ng h tr cc thi t b khc c a Apple ,ch ng h n nh iPad , iPod iOS bao g m h i u hnh v cc cng ngh m b n s d ng ch y cc ng d ng nguyn b n trn cc thi t b , ch ng h n nh iPad, iPhone, v iPod touch. M c d chia s m t thnht u chung v cc cng ngh c b n v i h i u hnh Mac OS X, IOS c thi t k p ng nhu c u c a m t mi tr ng di ng, n i m nhu c u c a ng i s d ng h i khc nhau. N u tr c b n pht tri n cc ng d ng cho Mac OS X, b n s tm th y nhi u cngngh quen thu c, nh ng b n c ng s tm th y cng ngh ch c trn iOS, ch ng h n nh giaodi n Multi-Touch v h tr gia t c. IOS SDK c ch a m, thng tin, v cc cng c b n c n pht tri n, ki m tra, ch y, g l iv i u ch nh cc ng d ng cho iOS. Cc cng c c a Xcode cung c p cc ch c n ng ch nh s a c b n, bin so n, v mi tr ng g r i cho m c a b n. Xcode c ng cung c p i m ram t th nghi m cc ng d ng c a b n trn m t thi t b iOS, v iOS Simulator-m t n nt ng m ph ng mi tr ng c b n iOS nh ng ch y trn my tnh Macintosh a ph ng c a b n.2. Ki n trc c a iOS Ki n trc iOS l t ng t nh ki n trc c b n trong Mac OS X. t ng cao nh t (toplayer), iOS ho t ng nh m t trung gian gi a ph n c ng c b n v cc ng d ng xu t hi n trnmn hnh. Cc ng d ng m b n t o ra hi m khi t ng tc tr c ti p v i ph n c ng c b n. Thayvo , cc ng d ng giao ti p v i ph n c ng thng qua m t t p h p cc cc giao di n h th ngc ng c xc nh b o v ng d ng c a b n t nh ng thay i ph n c ng. S tr u t ng nylm cho n d dng vi t cc ng d ng lm vi c nh t qun trn cc thi t b v i cc kh n ngh tr ph n c ng khc nhau. Figure 1-1 Applications layered on top of iOS Nhn c a iOS c d a trn nhn c a Mac c b n c tm th y trong h i uhnh Mac OS X. L p trn cng c a nhn ny l cc l p d ch v c s d ng

th c hi n cc ng d ng trn n n t ng ny.Hnh 1-2 cho th y m t ci nhn t ngquan c a cc l p

You might also like