IOS development (37): delegate in iphone Development

Source: Internet
Author: User

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];

 

The concept of delegate is closely related to model-view-controller, protocol, and single-line inheritance.
The main value of delegation is that it allows you to easily mimize 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 (execute a method call)

 

Protocol:

Let me explain my understanding. Object-c does not have many inheritance. So we need to avoid making a super class monster, huge, super, and waste object. A super class monster 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.


@ Protocal UITextFieldDelegate
-(BOOL) textFieldShouldReturn :( UITextField *) textField;
@ End

Delegate is always defined as assign @ property

@ Interface UITextField
@ Property (assign) id <UITextFieldDelegate> delegate;
@ End

(# Add. At this point, if you still cannot see that the bridge mode basically doesn't understand C ++, I think the word "delegate" is abused, it is not too common to include a class member variable and use it to call its member function. Except for basic type variables, it is common to include objects or pointers of another class, on this basis, the bridge model is upgraded to a level that seems ordinary and actually great)

 

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

// First, declare the Delegate in the interface
@ Interface delegateSampleViewController: UIViewController <UITextFieldDelegate>
{}
@ End

// Set the Delegate to self when initializing the implementation file)
@ Implementation delegateSampleViewController

//....
UITextField * textField = [[UITextField alloc] initWithFrame: CGRectMake (0, 0,300,400)];
TextField. delegate = self; // sets the current controller as the UITextField proxy, which is equivalent to registering (specifying) the proxy.
[TextField becomeFirstResponder];
[Cell. contentView addSubview: textField];
[TextField release];
//....

}

// Implement the behavior agreed in UITextFieldDelegate
# Pragma mark UITextFieldDelegate Method
// Called when 'Return 'key pressed. return NO to ignore.
-(BOOL) textFieldShouldReturn :( UITextField *) textField
{
[TextField resignFirstResponder];
Return YES;
}


 

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.