How to use DELEGATE
Hello everyone !! This post is about one of the feature in iOS programming called the DELEGATE. So lets look at the definition of delegate. "Delegate means passing message from one object to another object when an event happens" . Basically at the beginning, i understood nothing from the above definition, and i hope u feel the same now :-D . Lets dive into detail,i will explain it how.
Consider an real life object UIStudent . So a student does many things ,say like study,play,eat,bunk class etc . So the things the student can do are called the DELEGATES. An perfect delegate for the above example can be like this
//write the code here ,on what the student must do once he finished eating
Please don't panic on seeing the -(void) just ignore it now ,i will explain that in future posts.THE ABOVE CODING IS NOT AN ACTUAL CODE ,IT IS JUST A SELF-WRITTEN CODE TO MAKE YOU UNDERSTAND .In iOS programming the Delegates are not automatically called ,the programmer must give access . So How to give access ? . I will show it with an example. For inserting textfields in objective C ,we must call a function called the UITextField . Now we are going to create a textfield. My .h file looks like this
Now lets look at the .Xib file,
Search the textfield in the object list in the .xib window . Drag and drop into the viewcontroller . We must connect the textfield for giving the permission ,that is the IBOutlet feature.That is done by right clicking the FilewOwner(square yellow box) and selecting the textfield variable name ,in our case it is "text1",then connecting it to the field on the view controller. check the below image
As I have explained earlier ,the UITextField contains some delegates, like didfinishediting,didbeginediting etc . So to implement these delegate we need to give access . For that right click on the particular textfield ,select and drag the delegate option ,connect it to the FilesOwner. Shown in the image below .
Now the question is how to go the Delegate Window? !! . I will show you how. Just go the .h file or .m file or wherever u have used the function UITextField .Select it,right click on it ,and select the option Jump to Definition . As shown in the image below ,
This takes you the delegate window of your particular function ,in our case it is UITextField . Now the next question is ,why are we using the textfield delegate? . The answer is ,when u create a text field ,and when u execute it ,it ll be same like the image seen below
You could type something on the text box . But when u press enter ,you expect the keypad to hide or resign . But that wont happen . Because it is upto you to write the code for that, and for this purpose we need a delegate. Since it is associated with the textfield ,it is an textfield delegate.So now we need to find a piece of delegate which enables us to hide the keyboard when enter key is pressed. Similarly there are other delegates where you could hide the keyboard once you finished writing on the textfield and like-ways etc.Now lets look at the delegate window,
The above is the delegate window . Arrived by jumping to definition of the UITextField. Here you can select any delegate according to your needs. For this tutorial am going to select the last but not the least delegate ,the one which i have highlighted in the above image.Copy the code and paste it in the .m file as shown below
Now into the coding inside the delegate . Our delegate is just -(BOOL)textFieldShouldReturn:(UITextField *)textfield(Don't panic ,you will come to know how to trigger events/actions like this in future posts,just now take it as it is) , so open a curly brace and inside it we are writing a piece of code show in above image. we are mentioning [textField resignFirsteResponder] ,which means we are mentioning all the textfields to resign when enter key is pressed. and the next code is return YES ,so ,check the starting of delegate , it is -(BOOL) function ,this means the function is expecting a YES or NO from the user . Since we are going to implement this delegate in our program it is an YES. Thats it ,we have done everything needed ,just execute the project . check my output below, i wrote a text ,then pressed the enter key,and the keypad resigned
Thats it guys!! . You can check with other delegates for UITextField too . And in delegate window you will find to keywords @optional and @required ,for now just ignore those ,they are the protocols . We will deal with that later. Try the other delegates and for different outputs. Feel free to ask any doubts,either mail me or do comment !! Cheers !!!