Monday, December 29, 2008

More on the app delegate

In the last post I made where I discussed the UITableView I touched very briefly on the app delegate and how we can use that to access information we may want available to many different classes.

Basically, what we can do by using the code snippet to get the delegate is easily access information from wherever we are.  This is incredibly helpful for things like drill-downs.  To me, the main advantages of this approach are that you don't have to be passing information from class to class every time, and you can access the same information from all over your application very easily.

The way you can do this is actually fairly simple.  Firstly, import your app delegate's .h.  In our last example this was as simple as:

#import "TableViewPracticeAppDelegate.h"

And then one simple line and we're there!  Where you want to use this be sure to put this line of code:

TableViewPracticeAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

This is again assuming the last post's project/file names, but I assume everyone could easily modify this code to meet their own needs.  Since theArray was setup as a property in the app delegate, we were able to call it no problem.  If we wanted to get an object called "someItem" all we would need would be

[appDelegate someItem];
Hopefully this clears up any confusion in case anyone was struggling with that particular section of the UITableView Part I.

No comments:

Post a Comment