Iphone programming

Preview:

DESCRIPTION

Iphone programming

Citation preview

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 <phutq8014@setacinq.com.vn>

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.

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

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

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

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

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

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

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;

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)

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)

iPhone SDK

Xcode

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;

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

Views : Create viewInterface Builder

1

2

34

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

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

ViewControllerUIViewController: root class

Các loại Viewcontroller:UITabbarControllerUINavigationControllerUITableViewController

ViewController:UITabbarController Là Array các viewcontroller

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

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

UITabBarController+ UINavigationController

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

UITableView (plain style)

UITableView (Group style)

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

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;

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

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

Data Property Lists

SQLite

Core Data

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

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);

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

Recommended