32
iPhone Developer Basic Program Day 2 Objective-C 2.0 by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111 Saturday, June 1, 13

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

Embed Size (px)

DESCRIPTION

iOS Basic Development Day 2 , Objective-C 2.0 & iOS Framework by Eakapong Kattiya [email protected] www.ibluecode.com +66 086-673-2111

Citation preview

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

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

Page 5: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 6: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 7: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 8: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 9: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 10: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 11: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 12: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

การสืบทอดคลาส Inheritance & Subclass

1. คลาส ทุกตัวต้องมีการสืบทอดจาก คลาสแม ่(Super class) เช่น UILabel สืบทอดมาจาก UIView

2. ยกเว้น คลาส NSObject เนื่องจากเป็นคลาสแม่ของทุกตัว

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 14: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 15: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 16: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 17: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 18: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

Developing iOS Apps : Frameworks

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 19: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

Developing iOS Apps : Frameworks

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 20: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

Developing iOS Apps : Frameworks

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 21: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 22: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 23: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

Model-View-Controller (MVC)

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 24: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

Model-View-Controller (MVC)

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 25: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13

Page 26: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 27: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 28: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 29: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 30: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 31: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

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

Page 32: iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework

Reuseabilty

- Model ความถี่ของการนําไปใช้ใหม่ : บ่อย

- View ความถี่ของการนําไปใช้ใหม่ : ปานกลาง

- Controller ความถี่ของการนําไปใช้ใหม่ : น้อยมาก

by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Saturday, June 1, 13