สอนเขียน iphone application development เรื่อง delegation

20
Delegation

Upload: iosthcom

Post on 22-Nov-2014

107 views

Category:

Education


1 download

DESCRIPTION

สอนเรื่อง Delegation ซึ่งเป็นหนึ่งใน Design Pattern ที่สำคัญมากสำหรับการพัฒนาแอพ iPhone โดยนำตัวอย่างการใช้ UITextFieldDelegate และ UIAlertViewDelegate http://www.iOSTH.com

TRANSCRIPT

Page 1: สอนเขียน iPhone Application Development เรื่อง Delegation

Delegation !

Page 2: สอนเขียน iPhone Application Development เรื่อง Delegation

Delega&on  •  Objects  

–  Object  เจ้านาย,  Delega&ng  Object –  Object  ขี้ข้า,  Delegate  Object  

•  Object  เจ้านาย สั่ง delegate  object  ให้ทำงานต่อ  •  Object  เจ้านาย ต้องมี delegate  property  เพื่อแต่งตั้ง ขี้ข้า •  Object  ขี้ข้าต้องเตรียมรับ คำสั่ง จาก  Object  เจ้านาย

–  Delegate  Protocol

Page 3: สอนเขียน iPhone Application Development เรื่อง Delegation

Example:  UITextFieldDelegate  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

myUITextField  

Page 4: สอนเขียน iPhone Application Development เรื่อง Delegation

ละคร  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

myUITextField  

เจ้าจงเป็น delegate  ให้ข้า (แปลเพราะๆ ว่า มึงมาเป็นขี้ข้าให้กู)  

ข้าน้อย MyViewController  ขอรับใช้  

ดีมาก! อยากให้ทำอะไรแล้วจะบอก  

ข้าน้อยจะเตรียม methods  ไว้รับใช้  

จงไปอ่าน Delegate  Protocol  วิธีการเป็นขี้ข้าที่ดี  

Page 5: สอนเขียน iPhone Application Development เรื่อง Delegation

STEPS  

1.  เจ้านายแต่งตั้งขี้ข้า        self.myUITextField.delegate = self;//self คือ MyViewController!

2. ขี้ข้ายอมรับการเป็นทาส    @interface MyViewController : UIViewController <UITextFieldDelegate>!

3. ขี้ข้าเตรียม methods  ไว้รับใช้เจ้านาย  -(BOOL)textFieldShouldReturn:(UITextField *)textField{…} !

Page 6: สอนเขียน iPhone Application Development เรื่อง Delegation

Example:  UITextFieldDelegate  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

myUITextField  

Page 7: สอนเขียน iPhone Application Development เรื่อง Delegation

Example:  UITextFieldDelegate  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

1)  แจ้งไปยังขี้ข้า

ว่า user  กด return  myUITextField  

Page 8: สอนเขียน iPhone Application Development เรื่อง Delegation

Example:  UITextFieldDelegate  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

1)  แจ้งไปยังขี้ข้า

ว่า user  กด return  

2)  ขี้ข้าเช็คว่ามี  method  รับการกด return  

…  myUITextField  

Page 9: สอนเขียน iPhone Application Development เรื่อง Delegation

Example:  UITextFieldDelegate  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

1)  แจ้งไปยังขี้ข้า

ว่า user  กด return  

2)  ขี้ข้าเช็คว่ามี  method  รับการกด return  

…  textFieldShouldReturn  

A  method  in    delegate  protocol    

of  UITextFieldDelegate  

myUITextField  

Page 10: สอนเขียน iPhone Application Development เรื่อง Delegation

Example:  UITextFieldDelegate  

           

ขี้ข้า MyViewController  =  Delegate  Object  

เจ้านาย myUITextField  =  Delega&ng  Object  

1)  แจ้งไปยังขี้ข้า

ว่า user  กด return  

2)  ขี้ข้าเช็คว่ามี  method  รับการกด return  

…  textFieldShouldReturn  

A  method  in    delegate  protocol    

of  UITextFieldDelegate  

myUITextField  

Page 11: สอนเขียน iPhone Application Development เรื่อง Delegation

UITextFieldDelegate  Protocol  Overview  

 The  UITextFieldDelegate  protocol  defines  the  messages  sent  to  a  text  field  delegate  as  part  of  the  sequence  of  edi&ng  its  text.  All  of  the  methods  of  this  protocol  are  op&onal.  Managing  Edi9ng  •  –  textFieldShouldBeginEdi&ng:    •  –  textFieldDidBeginEdi&ng:    •  –  textFieldShouldEndEdi&ng:    •  –  textFieldDidEndEdi&ng:    Edi9ng  the  Text  Field’s  Text  •  –  textField:shouldChangeCharactersInRange:replacementString:    •  –  textFieldShouldClear:    •  –  textFieldShouldReturn:  

Page 12: สอนเขียน iPhone Application Development เรื่อง Delegation

Delegate  Protocol  

•  List  of  methods  ที่ Delegate  Object    – ต้องมี (required)  – อาจมี (op&onal)  

•  เพื่อรับใช้เจ้านาย (Delega&ng  Object)  

Page 13: สอนเขียน iPhone Application Development เรื่อง Delegation

Example  2  :  UIAlertViewDelegate  

           

MyViewController  =  Delegate  Object  myAlertView  =  Delega&ng  Object  

Page 14: สอนเขียน iPhone Application Development เรื่อง Delegation

STEPS  

1.  เจ้านายแต่งตั้งขี้ข้า        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"AlertView Title" ! " message:@”Please click a button" ! delegate: self ! cancelButtonTitle:@"Cancel" ! otherButtonTitles:@"OK", nil];!

2.  ขี้ข้ายอมรับการเป็นทาส    @interface MyViewController : UIViewController <UIAlertViewDelegate>!

3.  ขี้ข้าเตรียม methods  ไว้รับใช้เจ้านาย  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{…} !

Page 15: สอนเขียน iPhone Application Development เรื่อง Delegation

Example  2  :  UIAlertViewDelegate  

           

MyViewController  =  Delegate  Object  myAlertView  =  Delega&ng  Object  

Page 16: สอนเขียน iPhone Application Development เรื่อง Delegation

Example  2  :  UIAlertViewDelegate  

           

MyViewController  =  Delegate  Object  myAlertView  =  Delega&ng  Object  

1)  แจ้งไปยัง delegate

ว่า user  กดปุ่มไหน  

Page 17: สอนเขียน iPhone Application Development เรื่อง Delegation

Example  2  :  UIAlertViewDelegate  

           

MyViewController  =  Delegate  Object  myAlertView  =  Delega&ng  Object  

1)  แจ้งไปยัง delegate

ว่า user  กดปุ่มไหน  

2)  Delegate  เช็คว่ามี  method  รับการกดปุ่ม

…  

Page 18: สอนเขียน iPhone Application Development เรื่อง Delegation

Example  2  :  UIAlertViewDelegate  

           

MyViewController  =  Delegate  Object  myAlertView  =  Delega&ng  Object  

2)  Delegate  เช็คว่ามี  method  รับการกดปุ่ม

…  alertView:clickedButonAtIndex:  

1)  แจ้งไปยัง delegate

ว่า user  กดปุ่มไหน  

Page 19: สอนเขียน iPhone Application Development เรื่อง Delegation

Example  2  :  UIAlertViewDelegate  

           

MyViewController  =  Delegate  Object  myAlertView  =  Delega&ng  Object  

3)  MyViewController  ทำงานตามแต่เราจะโค้ด เช่น NSLog();  

2)  Delegate  เช็คว่ามี  method  รับการกดปุ่ม

…  alertView:clickedButonAtIndex:  

1)  แจ้งไปยัง delegate

ว่า user  กดปุ่มไหน  

Page 20: สอนเขียน iPhone Application Development เรื่อง Delegation

UIAlertViewDelegate  Protocol  Overview  

 The  UIAlertViewDelegate  protocol  defines  the  methods  a  delegate  of  a  UIAlertView  object  should  implement.  The  delegate  implements  the  buZon  ac&ons  and  any  other  custom  behavior.  Some  of  the  methods  defined  in  this  protocol  are  op&onal.  Responding  to  Ac9ons  •  –  alertView:clickedBuZonAtIndex:    Customizing  Behavior  •  –  alertViewShouldEnableFirstOtherBuZon:    •  –  willPresentAlertView:    •  –  didPresentAlertView:    •  –  alertView:willDismissWithBuZonIndex:    •  –  alertView:didDismissWithBuZonIndex:    Canceling  •  –  alertViewCancel: