开发者

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

开发者 https://www.devze.com 2023-03-12 01:44 出处:网络
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 actual

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 开发者_如何学PythonAppDeleagte 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.


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.


import "AppDelegate"

in implementation file of viewcontroller

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

[app inappDelegateDeclaredFunctionname];
0

精彩评论

暂无评论...
验证码 换一张
取 消