iphone programming

34
Lập trình ứng dụng iPhone SETA:CINQ Vietnam, Ltd - 3D Building, Nguyen Phong Sac Str, Cau Giay Dist, Ha Noi Truong Quoc Phu <[email protected]>

Upload: coderseta-jp

Post on 18-Nov-2014

2.264 views

Category:

Technology


2 download

DESCRIPTION

Iphone programming

TRANSCRIPT

Page 1: Iphone programming

Lập trình ứng dụng iPhone

SETA:CINQ Vietnam, Ltd - 3D Building, Nguyen Phong Sac Str, Cau Giay Dist, Ha Noi

Truong Quoc Phu <[email protected]>

Page 2: Iphone programming

Nội dung chínhMôi trường lập trình ứng dụng iphone

Ngôn ngữ Objective-c

iPhone SDK

Build & Distribute app trên device

Mục tiêu: giới thiệu cơ bản về về lập trình ứng dụng iphone, giúp mọi người có cái nhìn sơ qua và tiếp cận lập trình ứng dụng cho iphone một cách dễ dàng hơn.

Page 3: Iphone programming

Môi trường lập trình ứng dụng iPhoneNgôn ngữ lập trình ứng dụng iphone: Objective-C

Môi trường Hệ điều hành mac os Tools

http://developer.apple.com/

iPhone Simulator Interface Builder InstrumentsXcode & iPhone sdk

Page 4: Iphone programming

Objectiv-CLà ngôn ngữ hướng đối tượng, Cùng họ với c/c++ , nhưng

syntax khác với c/c++

@interface classname : superclassname { // instance variables

(type_1) proprety1;(type_2) proprety1;

} + classMethod1; + (return_type)classMethod2; \+ (return_type)classMethod3:(param1_type)param1_varName; -(return_type)instanceMethod1:(param1_type)param1_varName:(param2_type)param2_varName; -(return_type_object*)instanceMethod2WithParameter:(param1_type_object*)param1_varName andOtherParameter:(param2_type)param2_varName; @end

Classname.m#import “Classname.h “@implementation classname @synthesize proprety1;(id)hello { printf("Recipient says hello!\n"); return self; }-@end

Classname.h

Page 5: Iphone programming

Objectiv-C (2) Gọi hàm

[object method];

[object method:argument];

[object method:arg1 andArg:arg2];

Thuộc tínhfloat height = [person height];

float height = person.height;

[person setHeight:newHeight];

person.height = newHeight;

ObjectCreate :Create = Allocate + Initialize

Class *object = [[Class alloc] init];Memory Management:

Allocation Destruction

C malloc free

Objective-C alloc dealloc

Page 6: Iphone programming

Objectiv-C(3): Reference Counting

Every object has a retain count Defined on NSObject As long as retain count is > 0, object is alive and valid

+alloc and -copy create objects with retain count == 1

-retain increments retain count

-release decrements retain count

When retain count reaches 0, object is destroyed

-dealloc method invoked automatically

One-way street, once you’re in -dealloc there’s no turning back

Page 7: Iphone programming

Objectiv-C: Foundation Framework

Foundation FW: là FW phát triển trên objective-c. Cung cấp tất cả các lớp cơ bản : string, number, collection, file , Tasks, timers, threads , File system, pipes, I/O, bundles

NSObjectLà lớp rootQuản lý về bộ nhớ: init, dealloc

Page 8: Iphone programming

Objectiv-C: các kiểu dữ liệu cơ bảnNSString:

NSString *aString = @”Hello World!”;

NSString *log = [NSString stringWithFormat: @”It’s ‘%@’”, aString];

NSLog(@”%@”,log);

- (NSString *)stringByAppendingString:(NSString *)string;

- (NSString *)stringByAppendingFormat:(NSString *)string;

- (NSString *)stringByDeletingPathComponent;

- (BOOL)isEqualToString:(NSString *)string;

- (BOOL)hasPrefix:(NSString *)string;

- (int)intValue;

- (double)doubleValue

Page 9: Iphone programming

Objectiv-C: các kiểu dữ liệu cơ bản(2)NSArray:

+ arrayWithObjects:(id)firstObj, ...; - (unsigned)count;- (id)objectAtIndex:(unsigned)index;-(unsigned)indexOfObject:(id)object; NSMutableArray;

NSMutableArray: subclasses NSArray + (NSMutableArray *)array;- (void)addObject:(id)object;- (void)removeObject:(id)object;- (void)removeAllObjects;- (void)insertObject:(id)object atIndex:(unsigned)index;

Page 10: Iphone programming

NSDictionary: là kiểu dữ liệu quản lý Object theo key+ dictionaryWithObjectsAndKeys: (id)firstObject, ...;- (unsigned)count;- (id)objectForKey:(id)key;

- NSMutableDictionary: subclasses NSDictionary+ (NSMutableDictionary *)dictionary;

- (void)setObject:(id)object forKey:(id)key;

- (void)removeObjectForKey:(id)key;

- (void)removeAllObjects;

Objectiv-C: các kiểu dữ liệu cơ bản(3)

Page 11: Iphone programming

NSNumber: Trong objective-c có thể dụng các kiểu dữ liệu số cơ bản trong C: int,

float, double. NSNumber là kiểu dữ liệu số ở dạng object

+ (NSNumber *)numberWithInt:(int)value;

+ (NSNumber *)numberWithDouble:(double)value;

- (int)intValue;

- (double)doubleValue;

NSData / NSMutableData Arbitrary sets of bytes

NSDate / NSCalendarDateTimes and dates

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC

Objectiv-C: các kiểu dữ liệu cơ bản(4)

Page 12: Iphone programming

iPhone SDK

Xcode

Page 13: Iphone programming
Page 14: Iphone programming

ViewsUIView

1 hình khoảng vuông trên màn hình

Nội dung được hiển thị trên đó

Bắt các sự kiện

1 view có một superview

1 view không có hoặc có nhiều subview- (void)addSubview:(UIView *)view;

- (void)removeFromSuperview;

- (void)insertSubview:(UIView *)view atIndex:(int)index;

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)view;

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)view;

Page 15: Iphone programming

Views (2)• CGPoint: { x , y }• CGSize : { width , height }• CGRect: { origin , size }

Page 16: Iphone programming
Page 17: Iphone programming
Page 18: Iphone programming

Views : Create viewInterface Builder

1

2

34

Page 19: Iphone programming

Sử dụng code

CGRect frame = CGRectMake(0, 0, 200, 150);

UIView *myView = [[UIView alloc] initWithFrame:frame];

[window addSubview: myView ];

[myView release];

Views : Create view

Page 20: Iphone programming

View: các loại view cơ bản1. UILable

2.UIImageView

3.UITextField

4. UISlider

5.UISegmentedControl

6.UISwitch

7.UIButton

1

2

7

3

4

5

6

Page 21: Iphone programming

ViewControllerUIViewController: root class

Các loại Viewcontroller:UITabbarControllerUINavigationControllerUITableViewController

Page 22: Iphone programming

ViewController:UITabbarController Là Array các viewcontroller

Tabbar: điều hướng các viewcontroller

Page 23: Iphone programming

Là stack các viewcontroller

Navigation bar

Push to add a view controller

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

Pop to remove a view controller-

(void)popViewControllerAnimated:(BOOL)animated;

ViewController:UINavigationController

Page 24: Iphone programming

UITabBarController+ UINavigationController

Page 25: Iphone programming

UITableViewHiện thị dữ liệu dạng bảng

Xử lý điều hướng các chức năng chương trình

Page 26: Iphone programming

UITableView (plain style)

Page 27: Iphone programming

UITableView (Group style)

Page 28: Iphone programming

UITableViewDataSourceSố lượng section trong table

-(NSInteger)numberOfSectionsInTableView:(UITableView *)table

Số cell trong 1 section- (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section;

Hiện thị nội dung Cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

NSIndexPath: section , row

Page 29: Iphone programming

UITableView DelegateXử lý các sự kiện trên table: click cell, sửa cell, xóa cell

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath;

Page 30: Iphone programming

UITableViewControllerKhi tạo UITableViewController sẽ tạo luông UITableview và

kết hợp UITableViewDataSource và UITableView Delegate vào nội dung.

Page 31: Iphone programming

Data Property Lists

SQLite

Core Data

Page 32: Iphone programming

SqliteLà kiểu database nhỏ, nhẹ thường được dùng trong các ứng

dụng nhỏ, không cần sever.

Cấu trúc truy vấn theo chuẩn của sql

Quản lý file sqlite: add-on firefox SQLite Manager

Khi sử dụng thêm thư viện libsqlite3.dylib

Page 33: Iphone programming

sqlite Open Database

int sqlite3_open(const char *filename, sqlite3 **db);

Query DBsqlite3_stmt *statement;sqlite3_prepare_v2(db,[sqlString UTF8String],-1,&statement,nil);while (sqlite3_step(statement) == SQLITE_ROW) {

int a = sqlite3_column_int(statement, 0); //with numbernsstring *b = [NSString stringWithUTF8String:(char *)

sqlite3_column_text(statement, 1); //with string}sqlite3_finalize(statement);

Close DB sqlite3_close(db);

Page 34: Iphone programming

Build & DistributeCó tài khoản trên https://developer.apple.com/