How to call a function in a class from the AppDelegate

I am trying to call a function that is located in a class "ViewController" from the AppDelegate. The function uses a few IBOutlets connected to a view controller such as "label.text", etc. When I call this function from the AppDelegate by making a reference to ViewController, then running "ViewController.function", the app crashes when it gets to the IBOutlets in the function. I'm guessing that this function is running from the AppDelegate, and that is why it can't find the IBOutlets. Is there any way to call the function from the AppDelegate, but have it run in the ViewController class? Thanks in advance, any help is appreciated.

Accepted Reply

It depends at what time you call for the function in the class.


Notifications are very easy.


You declare a global name (outside of any class)

    public static let kNotification = Notification.Name("kNotification")


In AppDelegate, you post notification (instead of calling the function)

        let nc = NotificationCenter.default
        nc.post(name: .kNotification, object: nil)


In ViewController:

This notification is observed in you ViewController, at the end of viewDidload

       NotificationCenter.default.addObserver(self, selector: #selector(reactToNotification(_:)), name: .kNotification, object: nil)


And you define the action to execute:

    @objc func reactToNotification(_ sender: Notification) {
       // Do what you need, including updating IBOutlets
    }

@objc is needed here.

Replies

Are you sure the IBOutlets are instanciated when you call from AppDelegate ?

Is the func in ViewController a class function or an instance function ?


Another way is to post notification from AppDelegate and let the viewController handle it.

Yeah, the IBOutlets should be there when I call from the AppDelegate, as far as I know. The function is a regular class function, nothing special about it.


I didn't think of notifications, how would that work? (I'm relatively new to Swift so I don't know everything yet)

It depends at what time you call for the function in the class.


Notifications are very easy.


You declare a global name (outside of any class)

    public static let kNotification = Notification.Name("kNotification")


In AppDelegate, you post notification (instead of calling the function)

        let nc = NotificationCenter.default
        nc.post(name: .kNotification, object: nil)


In ViewController:

This notification is observed in you ViewController, at the end of viewDidload

       NotificationCenter.default.addObserver(self, selector: #selector(reactToNotification(_:)), name: .kNotification, object: nil)


And you define the action to execute:

    @objc func reactToNotification(_ sender: Notification) {
       // Do what you need, including updating IBOutlets
    }

@objc is needed here.

You need to be explict about what code you are using in your app delegate. This description is very vague:


>> I am trying to call a function that is located in a class "ViewController"


Is that a static function, or an instance function?


>> When I call this function from the AppDelegate by making a reference to ViewController,


What does "making a reference" mean? In most cases, the view controller instance (that has its outlets correctly set) already exists, and you need to find the reference to it.


For example, if you have code like this in your app delegate:


     let vc = ViewController ()
     vc.function ()


you will certainly crash, because this code creates a new ViewController object that does not have its outlets linked to anything (nor is it presented on the display, for that matter, which is another fatal problem).


>> then running "ViewController.function


Do you literally use this syntax:


     ViewController.function ()


or do you mean something else. This syntax almost certainly isn't what you want, unless you're calling a static function, in which case the reference to the view controller isn't needed or used.

Thanks so much! I got that to work very easily.

That is a very powerful mechanism.


Read documentation to know how tou use it properly.


In particular, you need to remove observer if you don'y need it anymore.