Marinos K Marinos K -4 years ago 139
iOS Question

calling ViewController's member function from AppDelegate

I want to call a

ViewController
's member function in
AppDelegate
's
applicationWillResignActive
function. Not sure what is the best way to do so. I tried doing so this way:

let landmark = LandmarkViewController()
landmark.test()


but it somehow doesn't seem right. I'm essentially creating new instance of the controller instead of using the already existent one.

Answer Source

I think the best way to achieve what you want is set an observer for this notification UIApplicationWillResignActiveNotification into your view controller itself.

override func viewDidLoad() {
    super.viewDidLoad()
    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appDidResign), name: Notification.Name.UIApplicationWillResignActive, object: nil)
}

func appDidResign() {
    // do your stuff
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download