Even in the most simple tutorial you can find the use of protocols. It will certainly happen to many in your view controller to use a protocol, inputting, next to the definition of the interface, a statement similar to:
1 2 3 | UIViewController <UIWebViewDelegate> { @ Interface myViewController: UIViewController {<UIWebViewDelegate> ... } |
The protocols (in the example above <UIWebViewDelegate> ) are a particular way of Objective-C to inherit the behavior of a class into another. In the example shown above our view controller manages a component probably UIWebView . When answering the events of the latter it incorporates the protocol UIWebViewDelegate .
, come ad esempio: Thus, the protocol implemented UIWebViewDelegate in our view controller, we can put in the file. m deployment methods (messages) that will be invoked by the component UIWebView , such as:
1 2 3 | void ) webViewDidFinishLoad : ( UIWebView * ) webView { - (Void) webViewDidFinishLoad: (UIWebView *) {WebView / / Load complete page } |
In other words, our view controller shares the interface <UIWebViewDelegate> , that inherits "some" feature.
Write your own protocol
. Imagine we have two classes: ClasseA and ClasseB . che, a sua volta, genera un messaggio quando accade un determinato evento. The class ClasseA instantiates inside the ClasseB which, in turn, generates a message when a specified event occurs. per rispondere e intercettare gli “eventi” rilasciati da ClasseB . We would then write a delegate for ClasseB that can be used by ClasseA to respond and intercept the "events" issued by ClasseB .
: The first thing to do is add the definition of the protocol in ClasseB along with a property (in the standard Apple) delegate :
1 2 3 4 5 6 7 8 9 10 11 | @ Protocol ClasseBDelegate <NSObject> @ Optional void ) mioEvento : ( id ) sender; - (Void) mioEvento: (id) sender; @ End NSObject { @ Interface ClasseB: NSObject { / / Other definitions id <ClasseBDelegate> delegated; } assign ) id <ClasseBDelegate> delegate; @ Property (assign) id delegate <ClasseBDelegate>; |
As a matter of course, in the file. M of ClasseB enter:
1 | @ Synthesize delegate; |
In the class implementation file ClasseB we can put the "fire" of our event this way:
1 2 3 | self.delegate != NULL && [ self.delegate respondsToSelector : @selector ( mioEvento : ) ] ) { if (self.delegate! = NULL & & [self.delegate respondsToSelector: @ selector (mioEvento:)]) { self ] ; [MioEvento delegate: self]; } |
: [self.delegate respondsToSelector:@selector(mioEvento:)] . First of all we verify that you have set a delegate: self.delegate != NULL and that this delegate provides, or is likely to respond to the message mioEvento : [self.delegate respondsToSelector:@selector(mioEvento:)] . ) che è il puntatore all'istanza della classe ClassB . If both conditions are met then invoke mioEvento in this example this event also includes a parameter ( id ) which is the pointer to the instance of the class ClassB .
: The class ClassA will not have to do is implement the delegate method and enter mioEvento :
1 2 3 4 5 6 7 |
In the implementation file when we create ClassB will use:
1 2 | [ ClasseB alloc ] ; ClasseB classeB * = [ClasseB alloc]; self ] ; [ClasseB setDelegate: self]; |
Then, insert:
1 2 3 | void ) mioEvento : ( id ) sender { - (Void) mioEvento: (id) sender { "mioEvento" ) ; NSLog (@ "mioEvento"); } |










Hello,
Article compliments!
I wanted to clarify my doubts about using ...
The delegate is usually invoked when an event happens in this case ... he created us "delegate" the method "mioEvento" must be invoked by hand?
I did not understand this step
Thank you.
@ Louis: Yes, the class that supports the protocol that we created will also have the task to invoke the delegate method. As shown above in our class the incident event will run code like:
2
3
self ] ; [MioEvento delegate: self];
}
That is, if you have set a delegate and that delegate support the requested method, then the method is executed.
In principle pesonal classes, of average complexity, must - this is the case - a supportatre about their protocol states and events of the class.
If you write any other questions as well.
Thank you ...
I'll give you one more question ... arises from the fact that I have not had a hand in the code and maybe shoot nonsense
I create my own protocol, and let's assume that the incident of that event I recall a particular method of the protocol as you have directed.
The implementation of the method will do in the course. M of my protocol
If I have to call a method that is part of my delegator class (ie one that uses the delegate) as proceed?
I have my own idea but I do not want to write bagianate
@ Louis: If I understand your question, you would normally find in front of two cases. Usually you have a view controller in addition to instantiate the object that supports a given protocol it is also a delegate. In this case when you write the method implementation and has direct access to the object instance that provides the protocol. This can be summarized as follows:
2
3
MyClass * object;
}
e, come detto sopra, se funzngerà anche da delegato per la
myClassAnell'implementazione del metodo avrà accesso alla variabileobjectA:myViewControllerinstanzierà the object of typemyClassAand, as mentioned above, if funzngerà also a delegate tomyClassAin the implementation of the method will have access to the variableobjectA:2
3
4
/ / Do something
; [Object myMethod];
}
With alternating In cases where there are the delegate of an object is a different class, such as:
2
3
4
5
6
...
myClassA alloc ] init ] ; object = [[myClass alloc] init];
[ [ myClassB alloc ] init ] ; myClassB ObjectBar * = [[myClassB alloc] init];
ObjectBar objectA.delegate =;
...
. In this case, a view controller instantiates the class
myClassApointing to this as a delegatemyClassB. che, ovviamente, non conosce a priopri il puntatore allamyClassA. The event raised bymyClassAwill be managed bymyClassBthat obviously does not know the pointer to prioprimyClassA.qualcuno deve passargliela; questo qualcuno è il view controller che conosce entrambe. In this case, if the instance of
myClassBmust interact with the instance ofmyClassAsomeone has to pass it, is the view that someone who knows both controllers. So the above code should be like this:2
3
4
5
6
7
...
myClassA alloc ] init ] ; object = [[myClass alloc] init];
[ [ myClassB alloc ] init ] ; myClassB ObjectBar * = [[myClassB alloc] init];
objectA ] ; // informa B di A [SetObjectAPointer ObjectBar: Object] / / informs B of A
ObjectBar objectA.delegate =;
...
If I understood the question ...
In the first case ...
I call the myMethod method that will obviously have an implementation.
Well ... in that we implement on behalf of an action that requires an invocation of a method or use of a variable present in my myViewController
How do I?
I hope that I explained!
[...] Elegant, clear and easy to use to deal with these situations. We have already spoken in Create a protocol with its own delegate. Objective-C allows you to define a communication protocol through which one or more classes [...]