简体   繁体   English

从视图控制器调用应用程序委托方法

[英]Call app delegate method from view controller

I want to know if I can call an app delegate method from another ViewController.我想知道是否可以从另一个 ViewController 调用应用程序委托方法。

When the app starts, the application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool i method is called.当应用程序启动时, application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool i 方法被调用。 Can I call this method a second time from another view controller?我可以从另一个视图控制器第二次调用这个方法吗?

Not sure why you want to do this.不知道你为什么要这样做。 You probably shouldn't, but for the purpose of answering the question here it is:你可能不应该,但为了回答这里的问题,它是:

// get a reference to the app delegate
let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate

// call didFinishLaunchWithOptions ... why?
appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)

In Swift 3.0, you can call as:在 Swift 3.0 中,您可以这样调用:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.anyAppDelegateInstaceMethod()

This method is called just once when app launches.该方法在应用启动时只调用一次。 You can't from ViewController.你不能从 ViewController。 Instead make user defined method in AppDelegete.而是在 AppDelegete 中创建用户定义的方法。 and call that method from ViewController.并从 ViewController 调用该方法。 By getting object of AppDelegate.通过获取 AppDelegate 的对象。

AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDel <Your method>];

Constructors:构造函数:

Add a constructor in AppDelegate Class at the end of code在 AppDelegate 类的代码末尾添加构造函数

Swift 3.x斯威夫特 3.x

 class func shared() -> AppDelegate
{
    return UIApplication.shared.delegate as! AppDelegate
}

Swift 2.x斯威夫特 2.x

func appDelegate () -> AppDelegate
{
return UIApplication.sharedApplication().delegate as! AppDelegate
}

and add a var like this并添加一个像这样的 var

var boolForFun = Bool()

How to use reference in your class?如何在课堂上使用引用?

Method方法

for swift 3x access functins or variables用于快速 3x 访问函数或变量

AppDelegate.shared().boolForFun = true

for else为别的

appDelegate().methodFoo()

Variable多变的

appDelegate().foo

Swift 4, Swift 5斯威夫特 4、斯威夫特 5

As others have said you shouldn't do that.正如其他人所说,你不应该这样做。 It would be better if you trigger it when you are in a certain application life cycle and do something specific using the Notification Center.如果您在某个应用程序生命周期中触发它并使用通知中心执行特定操作,那就更好了。

Example (in ViewController):示例(在 ViewController 中):

NotificationCenter.default.addObserver(
    self,
    selector: #selector(applicationWillEnterForeground),
    name: UIApplication.willEnterForegroundNotification,
    object: nil
)

However, if you do have to call the app delegate method, you can use this但是,如果您确实必须调用应用程序委托方法,则可以使用它

let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM