Subscribe ( )

iPhone Programming Tutorials

 

Forum

You must be logged in to post Login Register

Search 

UIApplication delagate…

User Post

11:02 pm
September 22, 2008


corey

iCoder

posts 10

1

I am having trouble understanding a line in one of the tutorials, the UITableView Fruit tutorial to be specific. 

The following line confounds me:

FruitAppDelegate *appDelegate = (FruitAppDelegate *)[[UIApplication sharedApplication ] delegate];


I don't have a strong understanding of delegates, which is my first problem.  I realize this gives me access to the FruitAppDelegate, but I don't know why.


The questions I have:

1. I imported the header “FruitAppDelegate.h”. why cant I just reference the fruit objects in my code if i know about them?  Can't I just type:

FruitAppDelegate.fruits.name  

to get access to the name? if not, why not?

2. Why am I talking to UIApplication?  I'm guessing it has magic access to all objects in the application?  If not, what does it have access to?  I looked at the documentation but couldn't figure out it's use.

3. Since I don't understand the message to UIApplication, I also don't understand the delegate message wrapped around it.  what is the interior message returning and what does the delegate message accomplish.


Thanks for any info, I like to make sure I understand all the code I come across.

11:39 am
September 23, 2008


Brandon

Admin

brandontreb

posts 73

2

Corey,

Let me try to shed some light on it…

1. When you do an import such as FruitAppDelegate.h that just allows you to “talk” to objects of that type.  It does not automatically give you access to an object built of that type. 

You can't just do FruitAppDelegate.fruits.name  because FruitAppDelegate referes to a type of an object not an instance of that object.  A could help you a lot.  In order to get reference to the fruits (or fruits.name) , we need to talk to an instance of a FruitAppDelegate object.  The FruitAppDelegate object gets built in your main.m. 

Each app should have one app delegate.  All this is, is a starting point for your application.  It's kind of like the glue that holds your app together.  main.m calls this to start the app.

2.  UIApplication is a reference to your global application space.  It contains many global variables (a reference to your apps appDelegate is one of them)

So what we are doing with that statement is just asking UIApplication to give us a reference to the appDelegate so that we may call methods on it and access its properties.

Also, you mentioned message passing.  Message passing is a nice way to say “calling a function” so when one object passes a message to another, it just calls one of its methods.

Let me know if you need any further explanation on this…Hope that helps

7:49 pm
September 23, 2008


corey

iCoder

posts 10

3

Thanks Brandon, that clears up a lot.

I am definitely mixing up my class definitions and instantiated objects….now I see that.

Your definition of UIApplication is much more clear than what I was reading in the documentation.

Just one final clarification… does UIApplication only have access to objects created in FruitAppDelegate.m? 

I did do Java and C++ several years ago, but recently I have been mostly working in C, Verilog, and MATLAB, so my OOP is a little rusty.   I'm mixing my OOP refresh with learning a new language on a new platform, so I am running into some growing pains.  Your tutorials are definitely helping,  so thanks again.

1:35 pm
October 7, 2008


VertigoSol

Moderator

posts 8

4

since FruitAppDelegate is using a protocol <UIApplicationDelagate>  (which is essentialy a java like interface) FruitAppDelegate has a funtion [[UIApplication sharedApplication] delegate] which gives a handle to the mainApplication in this case it is FruitAppDelegate


About the iCodeBlog forum

Currently Online:

2 Guests

Maximum Online: 14

Forums:

Groups: 2

Forums: 6

Topics: 118

Posts: 394

Members:

There are 225 members

There are 1 guests


Brandon has made 73 posts

Top Posters:

bobcubsfan - 33

crazyiez - 30

Uhu - 17

Nick - 15

corey - 10

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)  

Share and Enjoy: