lavelle
lavelle

Reputation: 1556

What's the purpose of the AppDelegate and ViewController files that Xcode generates?

I have written a few functioning iPhone applications, but mostly by following tutorials that tell you where to write the code. As a result I have gone without understanding what these files are actually designed to hold.

I made an app with code that is in the application method of the AppDeleagte file, so it gets called when the program runs. I wanted to make this code run on a button press instead, so I added a button, but when I came to put the code in the relevant IBAction, I realised all the variables and methods i needed were in the AppDelegate file, so I couldn't use them in the ViewController file.

So my immediate question is "how should I organise my code so that I can have it run on a button press?", but an explanation of the concepts behind it would be great too, becuase then I can do it without asking next time.

Upvotes: 1

Views: 2664

Answers (2)

vakio
vakio

Reputation: 3177

In short, the AppDelegate deals with application level events. Example: application becomes inactive, application starts etc. So whatever you need to setup when the application starts can go in there. As for your problem, I would suggest moving the variables to perhaps a singleton class or have another class that just contains variables and methods as a member in your appdelegate and do like madhu suggests. Having variables and methods directly in your app delegate works, but it can become big and nasty after a while.

Upvotes: 1

import "AppDelegate"

in implementation file of viewcontroller

AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];

[app inappDelegateDeclaredFunctionname];

Upvotes: 1

Related Questions