How to use iPhone app Delegate

Source: Internet
Author: User

IPhoneApplicationsDelegateThe Usage Details are the content to be introduced in this article. Let's learn quickly through an example. Let's look at the content first.

Here is an example:

Assume that one of my jobs is "answer the phone", but "I" finds that the call is too busy or too busy, so I hired a "secretary" to share my "Answer call" work. If the call was made by the boss, I asked the "secretary" to transfer the call to "I "...

Then, "I" is A Object. "secretary" is "I" "Delegate ". The written code is -- [My setDelegate: Secretary];

DelegateThe concept appears to be closely related to mvcmodel-view-controller), protocol, and single-line inheritance.

 
 
  1. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object. 

There are several methods to process events in Cocoa, one of which is that you can reload the corresponding event handler method in the class. For example, a MouseDown event is processed by the method mouseDown in the NSResponse class, therefore, all classes inherited from NSResponse can overload the mouseDown method to process the MouseDown event.

Another method is to use Delegate. When an object receives an event or notification, it queries its Delegate object to see if it can respond to the event or notification, if this object can be used, it will send a message to its Delegate object to execute a method call)

Protocol:

Let me explain my understanding. Object-c does not have many inheritance. So we need to avoid making an object. super class monster, huge, super, and waste) a super object itself denies the concept and meaning of object-oriented. To make the code more concise and organized, you can separate some of the duties.

The Protocol itself has no specific implementation. Only some interfaces that can be implemented by other classes are specified.

 
 
  1. @protocal UITextFieldDelegate    
  2. -(BOOL) textFieldShouldReturn:(UITextField *) textField ;    
  3. @end    
  4. @protocal UITextFieldDelegate  
  5. -(BOOL) textFieldShouldReturn:(UITextField *) textField ;  
  6. @end 

Delegate is always defined as assign @ property

 
 
  1. @interface UITextField    
  2. @property (assign) id<UITextFieldDelegate> delegate;    
  3. @end    
  4. @interface UITextField  
  5. @property (assign) id<UITextFieldDelegate> delegate;  
  6. @end  

In this way, we declare a delegate (delegate) in UITextField, so we need to delegate the proxy to implement the behavior agreed in UITextFieldDelegate.

 
 
  1. // First, declare the Delegate in the interface
  2. @ Interface delegateSampleViewController: UIViewController
  3. <UITextFieldDelegate> {}
  4. @ End
  5. // Set the Delegate to self when initializing the implementation file)
  6. @ Implementation delegateSampleViewController
  7. //....
  8. UITextField * textField = [[UITextField alloc] initWithFrame: CGRectMake (0, 0,300,400)];
  9. TextField. delegate = self; // sets the current controller as the UITextField proxy, which is equivalent to registering (specifying) the proxy.
  10. [TextField becomeFirstResponder];
  11. [Cell. contentView addSubview: textField];
  12. [TextField release];
  13. //....
  14. }
  15. // Implement the behavior agreed in UITextFieldDelegate
  16. # Pragma mark UITextFieldDelegate Method
  17. // Called when 'Return 'key pressed. return NO to ignore.
  18. -(BOOL) textFieldShouldReturn :( UITextField *) textField {
  19. [TextField resignFirstResponder];
  20. Return YES;
  21. }

Summary:IPhoneApplicationsDelegateI hope this article will help you understand the usage details!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.