简体   繁体   English

如何在应用程序委托 class 中从我的 controller class 访问插座?

[英]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.

相关问题 如何在应用程序启动时从应用程序委托加载不同的视图控制器类(即从应用程序委托) - How to load different view controller class from app delegate at the time of application launch(i.e from app delegate) 如何从app delegate访问导航控制器? - How do I access the navigation controller from app delegate? 如何从各种视图控制器访问存储在App Delegate中的数据? - How can I access data that's stored in my App Delegate from my various view controllers? iPhone:如何从另一个控制器类访问View控制器类中的数据? - iPhone: How I can Access Data in a View Controller Class from Another Controller Class? 如何在应用程序委托中访问我的班级的变量? - How to access a var of my class in the application delegate? 如何从另一个类访问委托方法? - How to access delegate method from another class? iPhone从UITableViewCell控制器访问父类/委托 - iPhone access parent class/delegate from UITableViewCell controller 如何从应用程序中另一个.xcodeproj中的类继承? - How can I inherit from a class that is in another .xcodeproj in my app? 如何在导航 controller 上调用另一个 class 并将其添加到应用程序委托 - How to call another class on navigation controller and add it to app delegate 如何从另一个iOS访问一个控制器的表视图委托方法 - How can I access a table view delegate methods of one controller from another ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM