![Page 1: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/1.jpg)
Delegation !
![Page 2: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/3.jpg)
Example: UITextFieldDelegate
ขี้ข้า MyViewController = Delegate Object
เจ้านาย myUITextField = Delega&ng Object
myUITextField
![Page 4: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/4.jpg)
ละคร
ขี้ข้า MyViewController = Delegate Object
เจ้านาย myUITextField = Delega&ng Object
myUITextField
เจ้าจงเป็น delegate ให้ข้า (แปลเพราะๆ ว่า มึงมาเป็นขี้ข้าให้กู)
ข้าน้อย MyViewController ขอรับใช้
ดีมาก! อยากให้ทำอะไรแล้วจะบอก
ข้าน้อยจะเตรียม methods ไว้รับใช้
จงไปอ่าน Delegate Protocol วิธีการเป็นขี้ข้าที่ดี
![Page 5: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/6.jpg)
Example: UITextFieldDelegate
ขี้ข้า MyViewController = Delegate Object
เจ้านาย myUITextField = Delega&ng Object
myUITextField
![Page 7: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/7.jpg)
Example: UITextFieldDelegate
ขี้ข้า MyViewController = Delegate Object
เจ้านาย myUITextField = Delega&ng Object
1) แจ้งไปยังขี้ข้า
ว่า user กด return myUITextField
![Page 8: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/8.jpg)
Example: UITextFieldDelegate
ขี้ข้า MyViewController = Delegate Object
เจ้านาย myUITextField = Delega&ng Object
1) แจ้งไปยังขี้ข้า
ว่า user กด return
2) ขี้ข้าเช็คว่ามี method รับการกด return
… myUITextField
![Page 9: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/12.jpg)
Delegate Protocol
• List of methods ที่ Delegate Object – ต้องมี (required) – อาจมี (op&onal)
• เพื่อรับใช้เจ้านาย (Delega&ng Object)
![Page 13: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/13.jpg)
Example 2 : UIAlertViewDelegate
MyViewController = Delegate Object myAlertView = Delega&ng Object
![Page 14: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/15.jpg)
Example 2 : UIAlertViewDelegate
MyViewController = Delegate Object myAlertView = Delega&ng Object
![Page 16: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/16.jpg)
Example 2 : UIAlertViewDelegate
MyViewController = Delegate Object myAlertView = Delega&ng Object
1) แจ้งไปยัง delegate
ว่า user กดปุ่มไหน
![Page 17: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/17.jpg)
Example 2 : UIAlertViewDelegate
MyViewController = Delegate Object myAlertView = Delega&ng Object
1) แจ้งไปยัง delegate
ว่า user กดปุ่มไหน
2) Delegate เช็คว่ามี method รับการกดปุ่ม
…
![Page 18: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/18.jpg)
Example 2 : UIAlertViewDelegate
MyViewController = Delegate Object myAlertView = Delega&ng Object
2) Delegate เช็คว่ามี method รับการกดปุ่ม
… alertView:clickedButonAtIndex:
1) แจ้งไปยัง delegate
ว่า user กดปุ่มไหน
![Page 19: สอนเขียน iPhone Application Development เรื่อง Delegation](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.pub/reader034/viewer/2022051609/546fd74ab4af9fff638b4621/html5/thumbnails/20.jpg)
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: