How to create your own protocol with delegated

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
# Import "ClasseB.h"

NSObject <ClasseBDelegate> { @ Interface ClassA: NSObject {<ClasseBDelegate>
...
}

void ) mioEvento : ( id ) sender; - (Void) mioEvento: (id) sender;

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");
}

6 comments to "How to create a protocol with its own representative"

  1. January 6, 2011 Louis:

    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.

  2. January 9, 2011 Giovambattista Fazioli :

    @ 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:

    1
    2
    3
    self.delegate != NULL && [ self.delegate respondsToSelector : @selector ( mioEvento : ) ] ) { if (self.delegate! = NULL & & [self.delegate respondsToSelector: @ selector (mioEvento:)]) {
    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.

  3. January 9, 2011 Louis:

    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 : D

    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

  4. January 9, 2011 Giovambattista Fazioli :

    @ 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:

    1
    2
    3
    UIViewController <myClassADelegate> { @ Interface myViewController: UIViewController {<myClassADelegate>
    MyClass * object;
    }

    e, come detto sopra, se funzngerà anche da delegato per la myClassA nell'implementazione del metodo avrà accesso alla variabile objectA : myViewController instanzierà the object of type myClassA and, as mentioned above, if funzngerà also a delegate to myClassA in the implementation of the method will have access to the variable objectA :

    1
    2
    3
    4
    void ) mioEvento { - (Void) {mioEvento
    / / Do something
    ; [Object myMethod];
    }

    With alternating In cases where there are the delegate of an object is a different class, such as:

    1
    2
    3
    4
    5
    6
    / / We are in a view controller
    ...
    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 myClassA pointing to this as a delegate myClassB . che, ovviamente, non conosce a priopri il puntatore alla myClassA . The event raised by myClassA will be managed by myClassB that obviously does not know the pointer to priopri myClassA .
    qualcuno deve passargliela; questo qualcuno è il view controller che conosce entrambe. In this case, if the instance of myClassB must interact with the instance of myClassA someone has to pass it, is the view that someone who knows both controllers. So the above code should be like this:

    1
    2
    3
    4
    5
    6
    7
    / / We are in a view controller
    ...
    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 ...

  5. January 10, 2011 Louis:

    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!

  6. March 10, 2011 Objective-C: Notifications and Delegates | Undolog.com :

    [...] 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 [...]

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA