SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
iPhone Developer Basic Program
Day 2 Objective-C 2.0
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, 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 eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
www.ibluecode.com/training.html
Day 1 - 5 Slide
www.slideshare.net/eakkattiya
Additional Course
eakkattiya@gmail.com
086-6732111
twitter.com/eakkattiya
facebook.com/eakapong.kattiya
Resources
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Object-Oriented Programming
Objective-C 2.0
Developing iOS Apps : Language
http://tinyurl.com/o54n8jd
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Objective-C 2.0
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
เราควรเริ่มเรียนรู้อะไรบ้าง ?
- Class & Object ( คลาส & ออปเจ็ค )
- Inheritance การสืบทอดคลาส / Subclass คลาสลูก
- Interface and Implementation
- Method & Property
- Alloc & init
Class & Object
Inheritance &
Subclass Interface &
Implementation
Method &
Property
Instantiation
Saturday, June 1, 13
Objective-C 2.0 : Class vs. Object
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +66 086-673-2111
// Compose a static array of string objects
NSString *objs[3] = {@"One", @"Two", @"Three"};
// Create an array object with the static array
NSArray *arrayOne = [NSArray arrayWithObjects:objs count:3];
// Create an array with a nil-terminated list of objects
NSArray *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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Objective-C 2.0 : Interface & Implementation
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Objective-C 2.0 : Interface (.h file)
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +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 eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
http://www.apple.com/ios/ios6/
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, 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 eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
Model
เป็น Class ที่สร้างขึ้นเพื่อใช้เก็บข้อมูลของโปรแกรม
เช่น Class Contacts เก็บรายชื่อ เบอร์โทร ผู้ติดต่อ
โดยไม่จําเป็นต้องคํานึงถึงView และ Controller
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
- View
เป็น Class ที่สร้างขึ้นเพื่อแสดงผลบนหน้าจอ เช่น UIView ,
UILabel , UITextField โดยไม่จําเป็นต้องคํานึงถึง Controller แต่ต้อง
คํานึงถึงประเภทของข้อมูลที่จะเชื่อมต่อกับ Model
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
- Controller
เป็น Class ที่สร้างขึ้นเพื่อเชื่อมต่อระหว่าง Model กับView เช่น
Class UIViewController และควบคุม Flow การทํางานของโปรแกรม
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
Decrease Spaghetti codes
เป็นการออกแบบโครงสร้างการทํางานของโปรแกรมเป็น 3 ส่วน
เพื่อลดความยุ่งเหยิงของ code ที่ link หรือพันกันไปมาใน Class
เดียวแบบเส้น สปาเกตตี
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
Avoid Monster Class
หลีกเลี่ยงการสร้าง Monster Class แบบ Class เดียวทําหน้าหลาย
อย่างตั้งแต่ เก็บข้อมูล แสดงผล ควบคุมView ต่าง ๆ ในตัวเอง
เนื่องจากไม่สามารถนํา code ไปใช้ใหม่ได้ง่าย
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Model-View-Controller (MVC)
- ( Easy to maintain ) โดยการแยกหน้าที่แต่ละส่วนชัดเจนช่วยให้
แก้ไขโปรแกรมได้ง่ายภายหลัง เนื่องจากมีส่วนเชื่อมต่อกันน้อยลง
- ( Reuseabilty) การแยกแต่ละส่วนชัดเจนช่วยให้สามารถเกิดการนํา
Code ไปใช้้ซ้ําใหม่ได้ หรือนําเอาไปใช้ที่อื่นได้
- การนําไปใช้ซ้ําใหม่ทําให้เขียน Code น้อยลง
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13
Reuseabilty
- Model
ความถี่ของการนําไปใช้ใหม่ : บ่อย
- View
ความถี่ของการนําไปใช้ใหม่ : ปานกลาง
- Controller
ความถี่ของการนําไปใช้ใหม่ : น้อยมาก
by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111
Saturday, June 1, 13

Más contenido relacionado

Más de Eakapong Kattiya

(31 July 2013) iOS Basic Development Day 2 Human interface design
(31 July 2013) iOS Basic Development Day 2 Human interface design (31 July 2013) iOS Basic Development Day 2 Human interface design
(31 July 2013) iOS Basic Development Day 2 Human interface design Eakapong Kattiya
 
Android basic 4 Navigation Drawer
Android basic 4 Navigation DrawerAndroid basic 4 Navigation Drawer
Android basic 4 Navigation DrawerEakapong Kattiya
 
Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Eakapong Kattiya
 
Android Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTAndroid Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTEakapong Kattiya
 
Iphone developer advance twitter
Iphone developer advance   twitterIphone developer advance   twitter
Iphone developer advance twitterEakapong Kattiya
 
iOS Advance Development - Social Media
iOS Advance Development - Social MediaiOS Advance Development - Social Media
iOS Advance Development - Social MediaEakapong Kattiya
 
Iphone developer advance location based
Iphone developer advance location basedIphone developer advance location based
Iphone developer advance location basedEakapong Kattiya
 

Más de Eakapong Kattiya (9)

(31 July 2013) iOS Basic Development Day 2 Human interface design
(31 July 2013) iOS Basic Development Day 2 Human interface design (31 July 2013) iOS Basic Development Day 2 Human interface design
(31 July 2013) iOS Basic Development Day 2 Human interface design
 
Android basic 4 Navigation Drawer
Android basic 4 Navigation DrawerAndroid basic 4 Navigation Drawer
Android basic 4 Navigation Drawer
 
Android basic 2 UI Design
Android basic 2 UI DesignAndroid basic 2 UI Design
Android basic 2 UI Design
 
Android basic 3 Dialogs
Android basic 3 DialogsAndroid basic 3 Dialogs
Android basic 3 Dialogs
 
Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )Evrdi : Social Diary ( iOS and Android )
Evrdi : Social Diary ( iOS and Android )
 
Android Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADTAndroid Basic Development Day 1 Introduction & ADT
Android Basic Development Day 1 Introduction & ADT
 
Iphone developer advance twitter
Iphone developer advance   twitterIphone developer advance   twitter
Iphone developer advance twitter
 
iOS Advance Development - Social Media
iOS Advance Development - Social MediaiOS Advance Development - Social Media
iOS Advance Development - Social Media
 
Iphone developer advance location based
Iphone developer advance location basedIphone developer advance location based
Iphone developer advance location based
 

iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

  • 1. iPhone Developer Basic Program Day 2 Objective-C 2.0 by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 2. 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 eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 3. www.ibluecode.com/training.html Day 1 - 5 Slide www.slideshare.net/eakkattiya Additional Course eakkattiya@gmail.com 086-6732111 twitter.com/eakkattiya facebook.com/eakapong.kattiya Resources by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 4. Object-Oriented Programming Objective-C 2.0 Developing iOS Apps : Language http://tinyurl.com/o54n8jd by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 5. Objective-C 2.0 by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 เราควรเริ่มเรียนรู้อะไรบ้าง ? - Class & Object ( คลาส & ออปเจ็ค ) - Inheritance การสืบทอดคลาส / Subclass คลาสลูก - Interface and Implementation - Method & Property - Alloc & init Class & Object Inheritance & Subclass Interface & Implementation Method & Property Instantiation Saturday, June 1, 13
  • 6. Objective-C 2.0 : Class vs. Object by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Class = แบบแปลนหรือพิมพ์เขียว/ชนิดของวัตถุ Object = วัตถุ Class ------ Implement -----> Object คลาส (แม่แบบ) ------ นําไปสร้างเป็น ---> วัตถุ Saturday, June 1, 13
  • 7. Objective-C 2.0 : Class vs. Object by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 UIView *myView ; ClassName *objectName ; Framework <UIKit> ขึ้นต้นด้วยตัวเล็กเสมอขึ้นต้นด้วยตัวใหญ่เสมอ Saturday, June 1, 13
  • 8. Objective-C 2.0 : Primitive Types (ตัวแปรเก็บค่าพื้นฐาน) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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
  • 9. Objective-C 2.0 : If Else Condition (เงื่อนไข) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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
  • 10. Objective-C 2.0 : NSArray (แก้ไขข้อมูลไม่ได้) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 // Compose a static array of string objects NSString *objs[3] = {@"One", @"Two", @"Three"}; // Create an array object with the static array NSArray *arrayOne = [NSArray arrayWithObjects:objs count:3]; // Create an array with a nil-terminated list of objects NSArray *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
  • 11. Objective-C 2.0 : NSDictionary (แก้ไขข้อมูลไม่ได้) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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
  • 12. การสืบทอดคลาส Inheritance & Subclass 1. คลาส ทุกตัวต้องมีการสืบทอดจาก คลาสแม่ (Super class) เช่น UILabel สืบทอดมาจาก UIView 2. ยกเว้น คลาส NSObject เนื่องจากเป็นคลาสแม่ของทุกตัว by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 13. Objective-C 2.0 : Interface & Implementation by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 14. Objective-C 2.0 : Interface (.h file) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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
  • 15. Objective-C 2.0 : Implementation (.m file) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 @implementation classname + (return_type)classMethod { // implementation } - (return_type)instanceMethod { // implementation } @end Saturday, June 1, 13
  • 16. Objective-C 2.0 : Messages (Method) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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
  • 17. Objective-C 2.0 : Instantiation (การสร้าง Object) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +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
  • 18. Developing iOS Apps : Frameworks by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 19. Developing iOS Apps : Frameworks by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 20. Developing iOS Apps : Frameworks by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 21. http://www.apple.com/ios/ios6/ Developing iOS Apps : Frameworks by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 22. 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 eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 23. Model-View-Controller (MVC) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 24. Model-View-Controller (MVC) by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 25. by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 26. Model-View-Controller (MVC) Model เป็น Class ที่สร้างขึ้นเพื่อใช้เก็บข้อมูลของโปรแกรม เช่น Class Contacts เก็บรายชื่อ เบอร์โทร ผู้ติดต่อ โดยไม่จําเป็นต้องคํานึงถึงView และ Controller by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 27. Model-View-Controller (MVC) - View เป็น Class ที่สร้างขึ้นเพื่อแสดงผลบนหน้าจอ เช่น UIView , UILabel , UITextField โดยไม่จําเป็นต้องคํานึงถึง Controller แต่ต้อง คํานึงถึงประเภทของข้อมูลที่จะเชื่อมต่อกับ Model by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 28. Model-View-Controller (MVC) - Controller เป็น Class ที่สร้างขึ้นเพื่อเชื่อมต่อระหว่าง Model กับView เช่น Class UIViewController และควบคุม Flow การทํางานของโปรแกรม by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 29. Model-View-Controller (MVC) Decrease Spaghetti codes เป็นการออกแบบโครงสร้างการทํางานของโปรแกรมเป็น 3 ส่วน เพื่อลดความยุ่งเหยิงของ code ที่ link หรือพันกันไปมาใน Class เดียวแบบเส้น สปาเกตตี by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 30. Model-View-Controller (MVC) Avoid Monster Class หลีกเลี่ยงการสร้าง Monster Class แบบ Class เดียวทําหน้าหลาย อย่างตั้งแต่ เก็บข้อมูล แสดงผล ควบคุมView ต่าง ๆ ในตัวเอง เนื่องจากไม่สามารถนํา code ไปใช้ใหม่ได้ง่าย by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 31. Model-View-Controller (MVC) - ( Easy to maintain ) โดยการแยกหน้าที่แต่ละส่วนชัดเจนช่วยให้ แก้ไขโปรแกรมได้ง่ายภายหลัง เนื่องจากมีส่วนเชื่อมต่อกันน้อยลง - ( Reuseabilty) การแยกแต่ละส่วนชัดเจนช่วยให้สามารถเกิดการนํา Code ไปใช้้ซ้ําใหม่ได้ หรือนําเอาไปใช้ที่อื่นได้ - การนําไปใช้ซ้ําใหม่ทําให้เขียน Code น้อยลง by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13
  • 32. Reuseabilty - Model ความถี่ของการนําไปใช้ใหม่ : บ่อย - View ความถี่ของการนําไปใช้ใหม่ : ปานกลาง - Controller ความถี่ของการนําไปใช้ใหม่ : น้อยมาก by Eakapong Kattiya www.ibluecode.com eak.k@ibluecode.com +66 086-673-2111 Saturday, June 1, 13