Wednesday, December 9, 2015

How to call an AppDelegate Method from ViewController using Objective C

One of the problem I faced recently was, when I needed to call a user defined method of AppDelegate class from the ViewController where I was working on. I did not want to repeat the same method in my project.

It is a bit different process to call a user defined method from an AppDelegate class from another ViewController class. But it is easy to do and will reduce the redundancy of code. You just need to write one (maximum two) line(s) to call the method from Delegate class.

The first procedure to call method from Delegate is like below:
[(AppDelegate*)[[UIApplication sharedApplication]delegate]method_name];

Another process can be as follows:
AppDelegate *delegateClass = (AppDelegate*)[UIApplication sharedApplication]delegate];
[delegateClass method_name];

By the second process you can also access the variable of the Delegate class.
For reading the value use this: delegateClass.VariableName;
k3And for changing value use this:    delegateClass.VariableName=@"VariableValue"

Even by the first process you can access the Delegate class variable. But you can only read the value of the variable, for editing you need to follow the second procedure. You can check the snippet of the these two procedures in Image 2(a) and Image 2(b).

Image 1(a): Declaring AppDelegate
Method and Variable as Public
Let's see how did I implement these two procedures in my project. For accessing the Delegate method and it's variable from other classes they must have to be public. The Image 1(a), on the right side, is the header file of Delegate class (here AppDelegate.h) where the properties, method "AppDelegateMethod" and variable "name", of this class are declared as public.

Image 1(b): Declaring and Using AppDelegate Method and Variable
In Image 1(b), we can see the Delegate method is declared with it's functionality and variable is initialized in "AppDelegate.m" file.






As the method and variable are public, so we can call them from any other class. Image 2(a) and Image 2(b) illustrates the calling process of these Delegate class properties from another ViewController class.

Image 2(a): Calling AppDelegate Properties without Creating
an object of AppDelegate Class

In the first process (Image 2a) we are calling the Delegate method and variable directly. That means we are not creating any object of Delegate class. By this process we can easily call the method and read variables, but changing variable value by this process is not possible.


Image 2(b) : Calling AppDelegate Properties by
Creating an object of AppDelegate Class


If it is necessary to change the value of the variable, we must create an object (Image 2b) of the AppDelegate class and then change the value.






Image 3 illustrates the outputs from AppDelegate Class (Image 3a) and from the ViewController Class where these processes are used. Image 3(b) and Image 3(c) are for first and second process respectively.

Image 3(a): Output from AppDelegateClass
Image 3(b): Output from ViewController Class
by using the first process
Image 3(c): Output from ViewController
Class by using the second process







While coding it is not only annoying to write the same methods but also creates confusion because of redundancy. By following the above processes the project will have less repetitive code.

No comments:

Post a Comment