iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
-
Upload
eakapong-kattiya -
Category
Technology
-
view
627 -
download
2
description
Transcript of iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iPhone Developer Basic Program
Day 2 Objective-C 2.0
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Course Outline
1. Introduction & Xcode
2. Objective-C & Frameworks
3. View & ViewController
4. View & ViewController (2)
5. Submit App Store
Course Outline
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
www.ibluecode.com/training.html
Day 1 - 5 Slidewww.slideshare.net/eakkattiya
Additional [email protected]
twitter.com/eakkattiya facebook.com/eakapong.kattiya
Resources
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Object-Oriented Programming
Objective-C 2.0
Developing iOS Apps : Language
http://tinyurl.com/o54n8jd
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Objective-C 2.0
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
เราควรเริ่มเรียนรู้อะไรบ้าง ?
- Class & Object ( คลาส & ออปเจ็ค )
- Inheritance การสืบทอดคลาส / Subclass คลาสลูก - Interface and Implementation
- Method & Property
- Alloc & init
Class & Object
Inheritance &
Subclass Interfac
e &
Implementati
on
Method &
PropertyInstan
tiation
Saturday, June 1, 13
Objective-C 2.0 : Class vs. Object
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Class = แบบแปลนหรือพิมพ์เขียว/ชนิดของวัตถุ
Object = วัตถุ
Class ------ Implement -----> Object
คลาส (แม่แบบ) ------ นําไปสร้างเป็น ---> วัตถุ
Saturday, June 1, 13
Objective-C 2.0 : Class vs. Object
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
UIView *myView ;
ClassName *objectName ;
Framework<UIKit>
ขึ้นต้นด้วยตัวเล็กเสมอขึ้นต้นด้วยตัวใหญ่เสมอ
Saturday, June 1, 13
Objective-C 2.0 : Primitive Types (ตัวแปรเก็บค่าพื้นฐาน)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
BOOL isCorrect = NO ; //YES
NSInteger myInteger = 1234 ;
CGFloat myFloat = 123.40 ;
double myDouble = 123.40 ;
Objective-C 2.0 : Value Object Types (ตัวแปรเก็บค่าพื้นฐาน + Method)
NSNumber *number = @1234 ; NSString *name = @"world" ; NSString *greeting = [NSString stringWithFormat:@"Hello, %@ , %d", name, [number integerValue]];
Saturday, June 1, 13
Objective-C 2.0 : If Else Condition (เงื่อนไข)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
NSString *name = @"world" ; BOOL isCorrect = [name isEqualToString:@"world"]; //Method if(isCorrect){ //Block
NSLog(@"Welcome %@",name);
}else{
NSLog(@"Wrong user name");}
Saturday, June 1, 13
Objective-C 2.0 : NSArray (แก้ไขข้อมูลไม่ได้)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
// Compose a static array of string objectsNSString *objs[3] = {@"One", @"Two", @"Three"}; // Create an array object with the static arrayNSArray *arrayOne = [NSArray arrayWithObjects:objs count:3]; // Create an array with a nil-terminated list of objectsNSArray *arrayTwo = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
NSArray *arrayThree = @[ @"Hello World", @67, [NSDate date] ];
// get second object in array @67 NSNumber *two = [arrayThree objectAtIndex:1];
Objective-C 2.0 : NSMutableArray (แก้ไขข้อมูลได้) NSMutableArray *mutableArray = [NSMutableArray new];[mutableArray addObject:@"First"];[mutableArray removeObjectAtIndex:0];
Saturday, June 1, 13
Objective-C 2.0 : NSDictionary (แก้ไขข้อมูลไม่ได้)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:@"eak",@"name", @10,@"age", YES,@"isPass", nil];NSString *name =[myDict valueForKey:@"name"] ;
Objective-C 2.0 : NSMutableDictionary (แก้ไขข้อมูลได้)NSMutableDictionary *myDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"eak",@"name", @10,@"age", YES,@"isPass", nil];[myDict setValue:@100 forKey:@"score"] ;NSString *score = [myDict valueForKey:@"score"];
Saturday, June 1, 13
การสืบทอดคลาส Inheritance & Subclass
1. คลาส ทุกตัวต้องมีการสืบทอดจาก คลาสแม ่(Super class) เช่น UILabel สืบทอดมาจาก UIView
2. ยกเว้น คลาส NSObject เนื่องจากเป็นคลาสแม่ของทุกตัว
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Objective-C 2.0 : Interface & Implementation
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Objective-C 2.0 : Interface (.h file)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
@interface classname : superclassname {
// instance variables NSString *userInput ; }
// Class Property@property (weak, nonatomic) IBOutlet UILabel *myLabel;@property (weak, nonatomic) NSString *name ;
+ classMethod1;
+ (return_type)classMethod2;
+ (return_type)classMethod3:(param1_type)param1_varName;
- (return_type) instanceMethod1With1Parameter:(param1_type)param1_varName;
- (return_type)instanceMethod2With2Parameters:(param1_type)param1_varName param2_callName:(param2_type)param2_varName;
@end
Saturday, June 1, 13
Objective-C 2.0 : Implementation (.m file)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
@implementation classname
+ (return_type)classMethod{ // implementation
}
- (return_type)instanceMethod{ // implementation
}
@end
Saturday, June 1, 13
Objective-C 2.0 : Messages (Method)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
@implementation classname
- (void)viewDidLoad{
[super viewDidLoad]; //Call Method [self changeColorToRed:5.0 green:2.0 blue:6.0];
}
// Method implementation- (void)changeColorToRed:(float)red green:(float)green blue:(float)blue{ UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
[self.view setBackgroundColor:color];
}
Saturday, June 1, 13
Objective-C 2.0 : Instantiation (การสร้าง Object)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
MyClass *myObject1 = [[MyClass alloc]init]; [myObject1 setName:@"Hello"]; myObject1.name = @"Hello";
MyClass *myObject2 = [MyClass new]; [myObject2 setName:@"Hello"]; MyClass *myObject3 = [[MyClass alloc]initWithString:@"Hello"];
Saturday, June 1, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
http://www.apple.com/ios/ios6/
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Developing iOS Apps : Design Patterns
Model-View-Controller
Target-Action
Delegation
Block Objects
Protocol
Notification Center
Key-Value Observing (KVO)
http://tinyurl.com/o8pnof9
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
Model
เป็น Class ที่สร้างขึ้นเพื่อใช้เก็บข้อมูลของโปรแกรม เช่น Class Contacts เก็บรายชื่อ เบอร์โทร ผู้ติดต่อ โดยไม่จําเป็นต้องคํานึงถึง View และ Controller
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
- View
เป็น Class ที่สร้างขึ้นเพื่อแสดงผลบนหน้าจอ เช่น UIView , UILabel , UITextField โดยไม่จําเป็นต้องคํานึงถึง Controller แต่ต้องคํานึงถึงประเภทของข้อมูลที่จะเชื่อมต่อกับ Model
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
- Controller
เป็น Class ที่สร้างขึ้นเพื่อเช่ือมต่อระหว่าง Model กับ View เช่น Class UIViewController และควบคุม Flow การทํางานของโปรแกรม
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
Decrease Spaghetti codes เป็นการออกแบบโครงสร้างการทํางานของโปรแกรมเป็น 3 ส่วนเพื่อลดความยุ่งเหยิงของ code ที่ link หรือพันกันไปมาใน Class เดียวแบบเส้น สปาเกตตี
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)Avoid Monster Classหลีกเลี่ยงการสร้าง Monster Class แบบ Class เดียวทําหน้าหลายอย่างตั้งแต่ เก็บข้อมูล แสดงผล ควบคุม View ต่าง ๆ ในตัวเอง เนื่องจากไม่สามารถนํา code ไปใช้ใหม่ได้ง่าย
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Model-View-Controller (MVC)
- ( Easy to maintain ) โดยการแยกหน้าที่แต่ละส่วนชัดเจนช่วยให้แก้ไขโปรแกรมได้ง่ายภายหลัง เนื่องจากมีส่วนเชื่อมต่อกันน้อยลง
- ( Reuseabilty) การแยกแต่ละส่วนชัดเจนช่วยให้สามารถเกิดการนํา Code ไปใช้้ซ้ําใหม่ได ้หรือนําเอาไปใช้ที่อื่นได้
- การนําไปใช้ซ้ําใหม่ทําให้เขียน Code น้อยลง
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13
Reuseabilty
- Model ความถี่ของการนําไปใช้ใหม่ : บ่อย
- View ความถี่ของการนําไปใช้ใหม่ : ปานกลาง
- Controller ความถี่ของการนําไปใช้ใหม่ : น้อยมาก
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13