(31 july 2013) ios basic development 1 introduction & xcode
DESCRIPTION
iOS Basic Development 1 introduction & Xcode , Eakapong Kattiya , iphone , iPadTRANSCRIPT
iOS Basic Development
Day 1
Xcode & Objective-C 2.0
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Course Outline
1. Xcode & Objective-C 2.0
2. Human Interface Design
3. TableViewController
4. Multimedia
5. Submit App Store
Course Outline
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 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-2111Thursday, June 13, 13
Developing iOS Apps
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Mobile Device in World Market
AndroidiOSWindows PhoneBlackBerryLinuxSymbian
credits: http://idc.com/
Worldwide Smartphone Operating Systems Market Share 1Q2013 (16 May 2013)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Mobile Device in World Market
Symbian1%Linux1%BlackBerry
3%Windows Phone3%
iOS17%
Android75%
AndroidiOSWindows PhoneBlackBerryLinuxSymbian
credits: http://idc.com/
Worldwide Smartphone Operating Systems Market Share 1Q2013 (16 May 2013)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
- It’s Wow ! (iPhone 5 / iPad Mini / iOS 7)
- Best Store (AppStore)
- Best Tools (Xcode , IB , Instruments )
- Best Support (documentation , sample code )
- Multi Platform (iPhone , iPad , iPod Touch)
- Advertising Program (iAds)
Why iOS ?
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
iMac
Macbook / Macbook Pro
Recommended for best performance
Macbook Air 13
ต้องมีเคร่ือง Mac ที่มี - OS Snow Leopard - CPU Intel ขึ้นไป - RAM 4 GB ขึ้นไป
Device
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Macbook Mac mini
Recommended for best value
เหมาะสําหรับผู้เริ่มต้นศึกษา
Device
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
- พัฒนาโปรแกรมเดียวสามารถนําไปใช้ได้บนอุปกรณ์หลายอย่างโดยแก้ไขปรับปรุงในส่วนของ Interface แต่ละอุปกรณ์เพียงเล็กน้อย
Multi Platform
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Developing iOS Apps : Setup
Download ชุดพัฒนา Xcode จาก > Lion : Mac App Store< Snow Leopard : developer.apple.com
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Developing iOS Apps : Tools
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Xcode Interface Builder iPhone Simulator
Dashcode Instruments
- Tool ในการพัฒนาโปรแกรมบนมือถือที่สะดวกและสมบูรณ์ที่สุดในปัจจุบัน เนื่องจาก Apple ออกแบบทั้ง Software และ Hardware เอง- iOS Simulator ที่ดีที่สุดใช้งานง่ายที่สุด
Tools : Xcode 4.6.3 / iOS 6.1 SDK
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Xcode : Navigator , Editor , Utility Area
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 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-2111Thursday, June 13, 13
Objective-C 2.0
เราต้องเริ่มเรียนรู้อะไรบ้าง ?
- การตั้งชื่อวัตถุ Class & Object ( คลาส & ออปเจ็ค )
- การจองหน่วยความจําให้วัตถุ Instantiation (Alloc & init)
- การสร้างคลาสใหม่ Inheritance / Subclass คลาสลูก - โครงสร้างคลาส Interface and Implementation :
- องค์ประกอบของคลาส Instance , Property & Method :
Class & Object
Instantiatio
n
(Alloc & Init)
Inheritance & Subclass
Interface &
Implementation
Instance , Property &
Method
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Class = แบบแปลนหรือพิมพ์เขียว/ชนิดของวัตถุ
Object = วัตถุ
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Class ------ Implement ------> Object
คลาส (แม่แบบ) ------ นําไปสร้างเป็น ------> วัตถุ
Objective-C 2.0 : การตั้งชื่อวัตถุ
Thursday, June 13, 13
UIView *myView ;ClassName *objectName ;
Framework<UIKit>
ขึ้นต้นด้วยตัวเล็กเสมอขึ้นต้นด้วยตัวใหญ่เสมอ
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
ปิดประโยค
pointer
Objective-C 2.0 : การตั้งชื่อวัตถุ
Thursday, June 13, 13
Objective-C 2.0 : Primitive Types (ตัวแปรเก็บค่าพื้นฐาน)
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]];
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
Objective-C 2.0 : การกําหนดค่าให้กับ Object
Thursday, June 13, 13
Objective-C 2.0 : If Else Condition (เงื่อนไข)
NSString *name = @"world" ; BOOL isCorrect = [name isEqualToString:@"world"]; //Method if(isCorrect){ //Block
NSLog(@"Welcome %@",name);
}else{
NSLog(@"Wrong user name");}
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : Instantiation (การจองหน่วยความจําให้ Object)
//This is variable type not object CGRect rect = CGRectMake(10, 10, 300, 200);
//Instantiation 1 UIView *myView1 = [[UIView new]; [myView1 setFrame:rect]; [myView1 setBackgroundColor:[UIColor greenColor]];
//Instantiation 2 UIView *myView2 = [[UIView alloc]init]]; [myView2 setFrame:rect]; [myView2 setBackgroundColor:[UIColor greenColor]];
//Instantiation 3 UIView *myView3 = [[UIView alloc]initWithFrame:rect]; [myView3 setBackgroundColor:[UIColor blueColor]];
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : Instantiation (การสร้าง Object)
//Instantiation 1
MyClass *myObject1 = [MyClass new]; [myObject1 setName:@"Hello"]; //Instantiation 2
MyClass *myObject2 = [[MyClass alloc]init]; myObject2.name = @"Hello";
//Instantiation 3
MyClass *myObject3 = [[MyClass alloc]initWithString:@"Hello"];
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : NSArray (แก้ไขข้อมูลไม่ได้)// 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];
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : NSDictionary (แก้ไขข้อมูลไม่ได้)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"];
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
การสืบทอดคลาส Inheritance & Subclass
1. คลาส ทุกตัวต้องมีการสืบทอดจาก คลาสแม ่(Super class) เช่น UILabel สืบทอดมาจาก UIView
2. ยกเว้น คลาส NSObject เนื่องจากเป็นคลาสแม่ของทุกตัว
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : Interface & Implementation
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : Interface (.h file)@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
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : Implementation (.m file)
@implementation classname
+ (return_type)classMethod{ // implementation
}
- (return_type)instanceMethod{ // implementation
}
@end
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
http://www.apple.com/ios/ios7/
Developing iOS Apps : Frameworks
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Create a new Xcode project
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Choose a template : iOS Application/ Single View Application
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Choose Product Name : MyFirstApp (ขึ้นต้นด้วยตัวใหญ่เสมอ)
Product Name : MyFirstApp
Class Prefix : ไม่ใส่
Devices : Universal
Use Storyboards : YES
Use ARC : YES
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
UIWindow
- ทุก Application จะต้องม ีwindow - บน iOS จะมีเพียง 1 window- ใช้สําหรับนํา ViewController(s) มาวางบน UIWindow
- ขนาด 320 x 480 (iPhone) 640 x 960 (iPhone Retina) 640 x 1136 (iPhone 5 Retina) 768 x 1024 (iPad) 1536 x 2048 (iPad Retina)
window
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
UIViewController
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
UIViewController
เป็น Class ที่ใช้สําหรับการเขียนคําสั่ง (Logic) เพื่อควบคุมการแสดง Views เช่นการสลับ Views แสดงข้อมูลบน View และลบ Views
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
UIViewController & UIView
-Control ที่ทําการแสดงผลที่เป็น Class UI (User Interfaces) นั้นสืบทอดมาจาก Class UIView เหมือนกันหมด เช่น UILabel , UITextField , UIButton
ดังนั้น UIViewController จึงสามารถเขียนคําสั่งควบคุม Control ได้ทุกตัว
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
// Method Declaration (instance method)- (void)changeColorToRed:(float)red green:(float)green blue:(float)blue; @end
Objective-C 2.0 : Method Declaration ( ViewController.h )
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Objective-C 2.0 : Method Implementation ( ViewController.m )
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
//Call instance 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];
}
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
UIView
Sub View (50,50,100,100)
Super View (10,10,300,200)
self.view (0,0,320,480)
- เป็นการวาดพื้นที่สี่เหลี่ยมเพื่อแสดงบนหน้าจอ- สามารถสร้างด้วย Interface Builder หรือ coding
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
UIView
- CGRect พื้นที่สี่เหลี่ยมในการวาด (x ,y ,width ,height)
- CGRectMake : (CGRect) คําสั่งในการสร้าง CGRect
- setBackgroundColor: (UIColor*) กําหนดสีพื้นหลัง
- addSubView : (UIView*) เอา View1 (sub View) หนึ่งไปแปะกับ View2 (ParentView)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
@implementation ViewController
- (void)viewDidLoad{ [super viewDidLoad];
//Call instance method //[self changeColorToRed:5.0 green:2.0 blue:6.0];
CGRect rect1 = CGRectMake(10, 10, 300, 200); UIView *myView1 = [[UIView alloc]init]; [myView1 setFrame:rect1]; [myView1 setBackgroundColor:[UIColor greenColor]]; [self.view addSubview:myView1]; CGRect rect2 = CGRectMake(50, 50, 100, 100); UIView *myView2 = [[UIView alloc]initWithFrame:rect2]; [myView2 setBackgroundColor:[UIColor blueColor]]; [myView1 addSubview:myView2];}
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
ViewController.m
Thursday, June 13, 13
UIViewController
- presentModalViewController แสดง ViewController ขึ้นมาทับ ViewController อื่น ๆ
- dismissModalViewControllerAnimated ยกเลิกการแสดง ViewController (ใช้คู่กับ presentModalViewController)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 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-2111Thursday, June 13, 13
Model-View-Controller (MVC)
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Model-View-Controller (MVC)
Model
เป็น Class ที่สร้างขึ้นเพื่อใช้เก็บข้อมูลของโปรแกรม เช่น Class Contacts เก็บรายชื่อ เบอร์โทร ผู้ติดต่อ โดยไม่จําเป็นต้องคํานึงถึง View และ Controller
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Model-View-Controller (MVC)
View
เป็น Class ที่สร้างขึ้นเพื่อแสดงผลบนหน้าจอ เช่น UIView , UILabel , UITextField โดยไม่จําเป็นต้องคํานึงถึง Controller แต่ต้องคํานึงถึงประเภทของข้อมูลที่จะเชื่อมต่อกับ Model
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Model-View-Controller (MVC)
Controller
เป็น Class ที่สร้างขึ้นเพื่อเช่ือมต่อระหว่าง Model กับ View เช่น Class UIViewController และควบคุม Flow การทํางานของโปรแกรม
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Model-View-Controller (MVC)
Decrease Spaghetti codes เป็นการออกแบบโครงสร้างการทํางานของโปรแกรมเป็น 3 ส่วนเพื่อลดความยุ่งเหยิงของ code ที่ link หรือพันกันไปมาใน Class เดียวแบบเส้น สปาเกตตี
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Model-View-Controller (MVC)Avoid Monster Classหลีกเลี่ยงการสร้าง Monster Class แบบ Class เดียวทําหน้าหลายอย่างตั้งแต่ เก็บข้อมูล แสดงผล ควบคุม View ต่าง ๆ ในตัวเอง เนื่องจากไม่สามารถนํา code ไปใช้ใหม่ได้ง่าย
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
Model-View-Controller (MVC)
- ( Easy to maintain ) โดยการแยกหน้าที่แต่ละส่วนชัดเจนช่วยให้แก้ไขโปรแกรมได้ง่ายภายหลัง เนื่องจากมีส่วนเชื่อมต่อกันน้อยลง
- ( Reusability) การแยกแต่ละส่วนชัดเจนช่วยให้สามารถเกิดการนํา Code ไปใช้้ซ้ําใหม่ได ้หรือนําเอาไปใช้ที่อื่นได้
- การนําไปใช้ซ้ําใหม่ทําให้เขียน Code น้อยลง
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBOutlet
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
IBOutlet
สร้างเพื่อกําหนด ชื่อตัวแปร ให้กับ Control ต่าง ๆ โดยประกาศใน Header File (ViewController.h)สําหรับการเขียน Code ใน Implementation (ViewController.m)
Thursday, June 13, 13
Storyboard : Design User Interface
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBOutlet : Insert new outlet : myLabelAssistant Editor
ViewController.h
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBOutlet : Insert new outlet : myLabel
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBOutlet : Insert new outlet : myTextField
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBOutlet : Insert new outlet : myTextField
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBAction
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111
IBAction สร้างเพื่อกําหนด ชื่อคําสั่ง ให้กับ Control ต่าง ๆ โดยประกาศใน Header File (ViewController.h) สําหรับการเขียน Code ใน Implementation (ViewController.m)
Thursday, June 13, 13
IBAction : UIButton Touch Up Inside : showName
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
IBAction : UIButton Target-Action : showName
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;@property (weak, nonatomic) IBOutlet UITextField *myTextField;
- (IBAction)showName:(id)sender;
@end
Interface file : ViewController.h
IBOutlet สร้างเพื่อกําหนด ชื่อตัวแปร ให้กับ Control ต่าง ๆ สําหรับการเขียน Code ใน ViewController.m
IBAction สร้างเพื่อกําหนด ชื่อคําสั่ง ให้กับ Control ต่าง ๆ สําหรับการเขียน Code ใน ViewController.m
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad{ [super viewDidLoad];! self.myLabel.text = @"What is your name?" ; }
#pragma mark UIButton Target-Action- (IBAction)showName:(id)sender { self.myLabel.text = [@"Welcome " stringByAppendingString:self.myTextField.text] ;}
#pragma mark TextField Delegate-(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES ;
}
Implementation file : ViewController.m
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13
ViewController.m
by Eakapong Kattiya www.ibluecode.com [email protected] +66 086-673-2111Thursday, June 13, 13