xcode 引用 AppDelegate 方法 - iphone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/851054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Referencing AppDelegate Methods - iphone
提问by jalo
I have an AppDelegate which has 3 views. I add all three
我有一个 AppDelegate,它有 3 个视图。我添加所有三个
[window addSubview:gameViewController.view];
[window addSubview:viewSettings.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
In the app delegate, i have some methodes for swapping views by calling
在应用程序委托中,我有一些方法可以通过调用来交换视图
[window bringSubviewToFront:gameViewController.view];
When i am inside viewController, I use
当我在 viewController 中时,我使用
pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate switchToSettings];
to switch my subviews...so far so good.
切换我的子视图...到目前为止一切顺利。
BUT, when I'm in my viewSetting UIViewController, and do the same appDelegate call, it chocks, like it doesn't understand how to call the appDelegate method.
但是,当我在 viewSetting UIViewController 中并执行相同的 appDelegate 调用时,它会卡住,就像它不了解如何调用 appDelegate 方法一样。
I've got all my views hooked in my mainwindow xib, but can't figure out why i can't traverse the methods in the main appdelegate
我的所有视图都挂在主窗口 xib 中,但不知道为什么我不能遍历主 appdelegate 中的方法
回答by Jane Sales
Inside viewController, you're setting up a local variable called appDelegate that points at your app delegate. That's what this line does:
在 viewController 中,您正在设置一个名为 appDelegate 的局部变量,该变量指向您的应用程序委托。这就是这一行的作用:
pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];
This variable is local to viewController, so you can't use it in the settings view controller. You need to set up another variable there.
这个变量是 viewController 的本地变量,所以你不能在设置视图控制器中使用它。您需要在那里设置另一个变量。
Alternatively, use this nice #define throughout your app. (You'll need to put it in a .h header file that you include in every file in your project.)
或者,在整个应用程序中使用这个不错的 #define。(您需要将它放在一个 .h 头文件中,该文件包含在项目中的每个文件中。)
#define myAppDelegate (MyAppDelegate *)[[UIApplication sharedApplication] delegate]
Then you can do the following anywhere:
然后您可以在任何地方执行以下操作:
[myAppDelegate doSomething];
回答by agough
For reference there is still a typo in the above, also I have another addition which helps avoid warnings of having to cast every time. if you use:
作为参考,上面仍然有一个错字,我还有另一个补充,它有助于避免每次都必须投射的警告。如果您使用:
#import "MyAppDelegate.h"
#define myAppDelegate (MyAppDelegate *) [[UIApplication sharedApplication] delegate]
I put the above in a constants.h and then can use
我把上面的放在一个constants.h然后可以使用
[myAppDelegate doSomething];
anywhere I import constants.h
我在任何地方导入 constants.h
回答by Cassie
Jane's answer had a typo in it.
简的回答有一个错字。
Use the following line in a header file (e.g. defines.h) that you import in every class:
在您在每个类中导入的头文件(例如,defines.h)中使用以下行:
#define myAppDelegate [[UIApplication sharedApplication] delegate]
You could also use the line in a prefix header file - this is an easy way of getting access to this everywhere.
您还可以在前缀头文件中使用该行 - 这是一种在任何地方都可以访问它的简单方法。
then in any method you can use any of the following:
那么在任何方法中,您都可以使用以下任何一种方法:
[myAppDelegate doSomething];
myAppDelegate.property=value;
myAppDelegate.childClassOfMyAppDelegate.property=value;
[myAppDelegate.ChildOfMyAppDelegate doSomething];