Tài liệu tham khảo về Lập trình Iphone: Game nhỏ với IPhone.
Trang 1Collection By traibingo 1
Bài 1: Giới thiệu các công cụ để lập
trình iPhone
Theo kinh nghiệm của tôi thì những người đã quá quen với hệ điều hành
windows và làm việc trên nền windows thì rất khó để mà thay đổi thói quen
sử dụng một hệ điều hành khác Tuy nhiên, cũng theo kinh nghiệm của tôi thì chỉ cần bỏ một thời gian ngắn chịu khó làm quen với cái mới thì ắt hẳn
sẽ thấy được nhiều điều thú vị mà bên windows không có Lấy một ví dụ
điển hình 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ó dùng, giao diện thì ko được bóng bẩy như windows 7, khi cần ứng dụng cho MAC thì tìm lè lưỡi cũng chẳng ra, mà có
ra thì cũng khó tìm được bản crack
Tất nhiên là cũng phải tuỳ vào tính chất công việc mà người ta nên chọn
hệ điều hành nào cho phù hợp Ở đây, công việc của chúng ta là lập trình ứng dụng trên iPhone nên MAC OS là một trong số các yếu tố mà bạn phải
có Ngoài ra, để lập trình được ứng dụng iPhone bạn cần có 1 bộ iPhone
SDK do Apple cung cấp, phiên bản mới nhất hiện nay là 3.1.2 và một bộ
XCode cùng phiên bản dùng để coding và kéo thả giao diện
iPhone SDK và XCode thì có thể dễ dàng download tại trang web của
Apple còn MAC OS thì tốt nhất là mua đĩa dành cho PC Việc cài đặt MAC
OS trên PC thực sự không phải dễ dàng chút nào vì theo như kinh nghiệm cài MAC của tôi thì mỗi máy PC có một dòng MAC OS riêng được hack để
có thể làm việc được trên nền phần cứng tương ứng Do đó mà khi bạn
chọn mua đĩa MAC nên chú ý đến cấu hình phần cứng của máy Chẳng
hạn như card âm thanh là do realtek sản xuất hay hãng khác, card đồ hoạ VGA hay Nvidia, card mạng, Chỉ cần bạn chọn cấu hình phần cứng lệch
một chút trong quá trình cài đặt thì coi như công cốc
Ngôn ngữ để lập trình ứng dụng trên iPhone là Objective-C Đây là một ngôn ngữ lập trình hướng đối tượng Tác giả đã phát triển kế thừa trình biên dịch C và tạo nên trình biên dịch mới cho Objective-C Vì vậy mà trình biên dịch Objective-C có thể biên dịch được cả ngôn ngữ C/C++ Trong lập trình iPhone bạn có thể viết cả 3 ngôn ngữ đó trong cùng một application
Trang 2Về XCode thì phải nói là quá tuyệt vời khi lập trình trên iPhone do nó hỗ trợ
rất mạnh cho iPhone Bạn sẽ ko cần phải quan tâm về các đối tượng UI
nữa Vì Xcode quản lý các đối tượng UI rất chặt chẽ Chẳng hạn như thay
vì bạn khai báo đối tượng UIButton thì bạn chỉ việc kéo đối tượng button vào View và ánh xạ nó tới biến UIButton trong mã nguồn nó sẽ tự động
khởi tạo khi load lên
Có lẽ chỉ một vài lời cũng khó mà diễn tả hết được cái hay của XCode
Thay vào đó bạn chỉ có thể trải nghiệm và cảm nhận
Bài tiếp theo chúng ta sẽ học về các đối tượng UI trong lập trình ứng dụng
trên iPhone và cấu trúc của chúng
Trang 3Collection By traibingo 3
Bài 2: UITableView Hello world
Trong bài này sẽ hướng dẫn cho các bạn cách tạo một ứng dụng "Hello
world" sử dụng UITableView
Mục tiêu của bài 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 let‟s get started
Learn About the Default Files
Trang 4What is all this stuff?
There are quite a few files that get added to your project At first glance,
this looks kind of intimidating Don‟t worry, we only need to edit one of
them Here is a quick explanation of the different files You don‟t 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
don‟t 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 don‟t 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 It‟s 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 won‟t 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 shouldn‟t 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 navigation-based 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
Trang 5Collection By traibingo 5
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 It‟s
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
Let’s write some code
Trang 6Start 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 Let‟s 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 don‟t 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"];
Trang 7Collection By traibingo 7
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
That‟s it! Click the Build and Go button again to launch the iPhone
simulator You should now see a screen like this:
Trang 8You 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
feel free to leave them in the comments Happy Xcoding!