Archive

Archive for the ‘Coding’ Category

AppDelegate –

January 10th, 2009

2007-06-11-14-57-24
Creative Commons License photo credit: sonicwalker

So, for the past few days I’ve been pulling my hair out (because I’m not a programmer so this is all new to me). I was having a really hard time with figuring out why even if I had a class named AppDelegate, and I had all the methods I needed in that AppDelegate class I would get exceptions thrown at runtime because I was requesting something my tableview controller that it didn’t have. Drove me bananas trying to figure out why the tableview controller was responding and throwing errors when I knew the AppDelegate class has the right stuff.

Turns out there was one little problem with my understanding of the Model-View-Controller concept – and its this:

You can have as many classes as you want, and you can call them all kinds of things including AppDelegate, but only one of them is actually going to be the app delegate – and it’s the one that has this little line in it:

@interface AppDelegate : NSObject <UIApplicationDelegate> {

// notice the part where it conforms to UIApplicationDelegate. That’s the key!

I gleaned this little jem of information in an explanation from (another) Brandon here:

The AppDelegate is basically your application’s controller. It is where execution begins (other than main.m). That line was put into place to access the AppDelegate object. This was to get a reference to the “fruits” array. The “fruits” array was declared inside of our application’s delegate.
Here it is line by line

FruitAppDelegate *appDelegate

// Declaring an object to hold the reference to the applicaiton’s appDelegate

(FruitAppDelegate *)

// We are casting the return value of the next phrase to a FruitAppDelegate

[[UIApplication sharedApplication] delegate]

// This gives us a reference to the appDelegate for the whole application

now that the appDelegate object holds a reference to the application’s appDelegate object, we can access variables that are declared inside of it. This allows us to do things like [appDelegate.fruits count] which will return the number of fruits in the array.
I hope that helps…

So they key to all of this – is AppDelegate isn’t that class you smartly named AppDelegate. The AppDelegate was defined when you started the project.

brandon Coding

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.