Delegations - "Its basically a design pattern !!"
Link: Apple Docs for Delegations
You can download the source code here .
Let us suppose a scenario:
There is a View Controller which contains list of available restaurants near your location and now you want to filer the list of restaurants on basis of some parameters like nearby restaurants.
To achieve the above stated scenario what you will do is:
This is how your interface file looks like.
Now as a next step we will
1. Create A View Controller which contains list of restaurants. (ListOfRestoTVC)
2. Create another View Controller which contains the interface for parameters. (FilterViewController)
-- Now you need filter the list of restaurants on the basis of filter applied--
Here comes the concept of delegations where you create a protocol (Filter Protocol) which have some methods in it .
Now your view controller will add this protocol and will implement it in your implementation file and you need to define all the required methods functionality in the implementation file.
Link: Apple Docs for Delegations
You can download the source code here .
Let us suppose a scenario:
There is a View Controller which contains list of available restaurants near your location and now you want to filer the list of restaurants on basis of some parameters like nearby restaurants.
To achieve the above stated scenario what you will do is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @protocol myFilter <NSObject> -(void)allNearRestos; -(void)allFarRestos; -(void)allRestos; @end // Protocol is defined above the import statment to avoid cyclic redundancy #import <UIKit/UIKit.h> #import "Resto.h" #import "FilterViewController.h" // #IMP @interface ListOfRestosTVC : UITableViewController<UITableViewDataSource,UITableViewDelegate,myFilter> // Added myFilter protocol to this class. @end |
This is how your interface file looks like.
1. Create A View Controller which contains list of restaurants. (ListOfRestoTVC)
2. Create another View Controller which contains the interface for parameters. (FilterViewController)
-- Now you need filter the list of restaurants on the basis of filter applied--
Here comes the concept of delegations where you create a protocol (Filter Protocol) which have some methods in it .
Now your view controller will add this protocol and will implement it in your implementation file and you need to define all the required methods functionality in the implementation file.
The last step is while moving form main list of restaurants to the filter page you need to assign delegations stuff.
What you will do is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"filterVCSegue"]) { FilterViewController * fvc= (FilterViewController*)segue.destinationViewController; // If you are using storyboards then please don't alloc init with the VC intitalization this won't work. // #IMP fvc.myCustomDelegate=self; // myCustom Delegate is an 'id' type object and a property of filterviewcontroller class. // self means current class. // as a whole this statement means you are assigning the current class to the object of filter class and call call method of current class (i.e List of restaurents) } } |
Now we move on the Filter View Controller.
Here we will create one id type object who will confirm the protocol (myFilter)
@property (nonatomic,assign) id<myFilter>myCustomDelegate;
// this object will follow myFilter protocol
As we have already assigned it while transiting from List to Filter View Controller.
Now we can use all the protocol methods of List View Controller class with this delegate and can perform operations in the List VC .
1 2 3 4 5 6 7 8 9 10 | - (IBAction)allNear:(id)sender { // its an action for a button Clicked. NSLog(@"My Custom Delegate %@",_myCustomDelegate); [_myCustomDelegate allNearRestos]; // Remeber we have assigned myCustomDelegate in prepare of Segue Method to SELF. As self was the ListOfRestoTVC so we can call methods AllNearREsto which is listed in the protocol only. [self dismissViewControllerAnimated:YES completion:nil]; } |
The method "allNearRestos" is defined in ListView Controller and we are calling this method from FilterViewController.
This is how there 2 classes are connected and this is beauty of delegations.
You can download the source code here .
This is how there 2 classes are connected and this is beauty of delegations.
You can download the source code here .
Cheers.







