iphone programming
DESCRIPTION
Iphone programmingTRANSCRIPT
![Page 1: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/12.jpg)
iPhone SDK
Xcode
![Page 13: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/13.jpg)
![Page 14: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/15.jpg)
Views (2)• CGPoint: { x , y }• CGSize : { width , height }• CGRect: { origin , size }
![Page 16: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/16.jpg)
![Page 17: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/17.jpg)
![Page 18: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/18.jpg)
Views : Create viewInterface Builder
1
2
34
![Page 19: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/21.jpg)
ViewControllerUIViewController: root class
Các loại Viewcontroller:UITabbarControllerUINavigationControllerUITableViewController
![Page 22: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/22.jpg)
ViewController:UITabbarController Là Array các viewcontroller
Tabbar: điều hướng các viewcontroller
![Page 23: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/24.jpg)
UITabBarController+ UINavigationController
![Page 25: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/26.jpg)
UITableView (plain style)
![Page 27: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/27.jpg)
UITableView (Group style)
![Page 28: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/29.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/30.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/31.jpg)
Data Property Lists
SQLite
Core Data
![Page 32: Iphone programming](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/32.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/33.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070301/546b1c49af795902048b504c/html5/thumbnails/34.jpg)
Build & DistributeCó tài khoản trên https://developer.apple.com/