Objective-CMediumDelegateAndProtocolIs the content to be introduced in this article,DelegateAndProtocolThere are two concepts. The protocol is actually equivalent to the pure virtual class concept in C ++. It can only be defined and implemented by other classes. Delegation is similar to interfaces in Java.Objective-CThe mechanism for implementing delegation is implemented using protocols. I think this is not true for the following reasons :).
The Objective-C delegation does not have any relationship with the Protocol. As described above, the Protocol serves as a pure virtual class in C ++, and does not have any relationship with the protocol for "delegation, however, we often use the Protocol to implement the delegation mechanism. In fact, we can implement delegation without the protocol. The implementation method is as follows:
Define A Class:
- @interface A:NSObject
- -(void)print;
- @end
- @implement A
- -(void)print{
- }
- @end
Define A class B. In B, define an instance of Class A as A member variable in Class B:
- @interface B:NSObject{
- A *a_delegate;
- }
- @end
The following describes how to implement the delegate mechanism in the mai () function:
- void main()
- {
- B *b=[[B alloc]init];
- A *a=[[A alloc]init];
- b.a_delegate=a;
- [b.a_delegate print];
- }
In this way, the most basic delegation mechanism is complete. To apply the most common explanation, B needs to complete a print operation, but he does not implement this operation himself, instead, it is handed over to A to complete the operation. You only need to call the print operation implemented in A as needed.
Next we will write another implementation method, which is closer to the Protocol implementation method we usually see:
We still define A Class:
- @interface A:NSObject{
- B *b;
- }
- -(void)print;
- @end
- @implement A
- @synasize delegate;
- -(void)viewDidLoad{
- b=[[B alloc]init];
- b.delegate=self;
- }
- -(void)print{
- NSLog(@"print was called");
- }
- @end
Then change the definition of Class B to the following:
- @ Interface B: NSObject {
- Id delegate
- }
- @ Propert (nonamtic, retain) id delegate;
- @ End
-
- Now we don't need the main () function to implement the delegate mechanism in the implementation part of B:
-
- @ Implement B
- -(Void) callPrint {
- [Self. delegate print];
- }
- @ End
The above implementation method is the same as the first one, but the first is to call the delegate method in a third-party function. Delegate is an id type. In this example, it is an instance of Class A. Of course, you can call print in Class. The second method does not include third-party functions. It is to call methods in Class A in Class B. In other words, the print method is required in B, which is not implemented by yourself. Let A implement it and call it by yourself.
Next, we will discuss the most common way to implement delegation using protocols, as described below:
Protocol-the protocol is used to follow the protocol, and the implementation method required by the protocol must be implemented.
Delegate-delegation, as its name implies, is to entrust others to handle affairs, that is, when one thing happens, it will not be handled by others.
When A view contains B view
B view needs to modify the view interface, so delegation is required at this time.
Several steps are required
1. First define an agreement
2. Methods of a view Implementation Protocol
3. B view sets a delegate variable.
4. Set the delegate variable of B view to a view, which means that B view delegates a view to do things.
5. After an event occurs, use the delegate variable to call the Protocol method in a view.
Example:
- B _View.h:
- @ Protocol UIBViewDelegate <NSObject>
- @ Optional
- -(Void) ontouch :( UIScrollView *) scrollView; // declares the Protocol method.
- @ End
- @ Interface BView: UIScrollView <UIScrollViewDelegate>
- {
- Id <UIBViewDelegate> _ touchdelegate; // sets the delegate variable.
- }
- @ Property (nonatomic, assign) id <UIBViewDelegate> _ touchdelegate;
- @ End
- B _View.mm:
- @ Synthesize _ touchdelegate;
- -(Id) initWithFrame :( CGRect) frame {
- If (self = [super initWithFrame: frame]) {
- // Initialization code
- _ Touchdelegate = nil;
- }
- Return self;
- }
- -(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event
- {
- [Super touchesBegan: touches withEvent: event];
- If (_ touchdelegate! = Nil & [_ touchdelegate respondsToSelector: @ selector (ontouch :)] = true)
- [_ Touchdelegate ontouch: self]; // call protocol delegate
- }
-
- @ End
- A_View.h:
- @ Interface AViewController: UIViewController <UIBViewDelegate>
- {
- BView * m_BView;
- }
- @ End
- A_View.mm:
- -(Void) viewWillAppear :( BOOL) animated
- {
- M_BView. _ touchdelegate = self; // sets the delegate
- [Self. view addSubview: m_BView];
- }
- -(Void) ontouch :( UIScrollView *) scrollView
- {
- // Implementation Protocol
- }
Summary: DetailsObjective-CMediumDelegateAndProtocolI hope this article will help you with the introduction!