Mindmajix

iPhone Developement – Objective C : Protocols

Protocols

A protocol is a group of related properties and methods that can be implemented by any class.

Protocols are nothing but interface in other language. The Protocol has only method declaration. There are optional methods and required methods. If your class adopts the protocol, then you have to implement those methods in your class.

     Apple provided mainly pre-defined protocol for UIKIT  framework, and we can also create over own protocols.

Steps to create a protocol:

Right click Screenshot_1806 objective c protocols

Right click on the classes  Screenshot_1806 add

                                                      Screenshot_1807

                                                   New file

                                                        Screenshot_1807

                                                   Cocava touch classes

                                                            Screenshot_1807

                                                   Objective c protocols

                                                               Screenshot_1807

                                                      Click on next

                                                                 Screenshot_1807

                                                    Give name to the protocol

                                                                 Screenshot_1807

                                                             Finish.

Syntax:-

    # import <UIKIT/ UIKIT.H>
   @protocol sample protocol;
  @ optional
   -(void) addition;        // any method declaration;
@required
   -(void) subtraction;
@ end

Implementation of protocols in a class:-

Like class interfaces, protocols typically reside in a .h file.

. h file:

# import <UIKIT/UIKIT .h>
# import “sample protocol’s . h”
#import    “second protocol’ . h”
@ interface protocol view controller: UI view controller
<Sample protocol, second protocol>
  {
  }
@end

. m file:-

  # import “protocol view controller’s .h”
@ implementation protocol view controller
 -(void) subtraction
{
}
-(void) addition
{
}
@end

Note: – No need to declare a protocol method in .h file.

Selector: –

A selector is the name of a method. A selector, by itself doesn’t do anything. It simply identifies a method.

In objective c, selectors are used to refer name of a method. The key word for selector I “@selector”

  Ex:-  @ selector (method name)

Passing method as argument

      [Self addition :@selector (subtraction)];

Checking whether method is present or not in the other class,

B*b  = [[ B alloc]init];
If ([b respond selector: @selector (subtraction)]]
     [b subtraction];
     else
       NsLog( “@” no subtraction method in the B class);

Note: –

@ selector is a unique identity to recognize the method name.

 

 


 

0 Responses on iPhone Developement – Objective C : Protocols"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.