[英]How can I access an outlet from my controller class in the app delegate class?
I want to change an image of an UIImageView from the applicationDidFinishLaunching method of the app delegate class.我想从应用程序委托 class 的 applicationDidFinishLaunching 方法更改 UIImageView 的图像。 But I am afraid that this class doesnt know much about the controller outlet.但恐怕这个class对controller插座了解不多。 What must I do in this case?在这种情况下我必须做什么?
You have to define the ViewController that the UIImageView is on, to the AppDelegate Class, so the applicationDidFinishLaunching can reference it.您必须将 UIImageView 所在的 ViewController 定义到 AppDelegate Class,以便 applicationDidFinishLaunching 可以引用它。
In the AppDelegate add @class whateverViewController before the interface statement, and define the instance.在 AppDelegate 中,在接口语句前添加@classwhateverViewController ,并定义实例。
#import <UIKit/UIKit.h>
@class WhateverViewController;
@interface WebDemoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
WhateverViewController *whateverViewController;
}
Now your app has a reference to whateverViewController.现在您的应用程序引用了whateverViewController。 You can then call a method in whateverViewController to change the UIImageView or set the UIImageView directly.然后您可以调用whateverViewController 中的方法来更改UIImageView 或直接设置UIImageView。 Of course this assumes that the whateverViewController is instantiated (eg alloc/init or loaded from nib);当然,这假设whateverViewController 已实例化(例如alloc/init 或从nib 加载); before you try to set anything, it must exist.在你尝试设置任何东西之前,它必须存在。
You would probably need to put an outlet for your UIImageView or its controller in your application delegate to make it accessible there.您可能需要在您的应用程序委托中为您的 UIImageView 或其 controller 放置一个插座,以使其可以在那里访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.