How to Call a View Controller Function from an iOS Appdelegate

How to call method in view controller in app delegate?

if its the root its easy:

(window.rootViewController as? ViewController)?.myFunction()

if its on top of a navigationController

((window.rootViewController as? UINavigationController)?.topViewController as? ViewController)?.myFunction()

But in either case its weird and probably not the right thing to do. Use NotificationCenter instead and post a notification name in your AppDelegate and then have the ViewController listen for messages and respond accordingly.

How can I call a view controller function from an iOS AppDelegate?

If you want to show a new view controller modally, then look into rach's answer further.

If you're ViewController is already on screen, you will need to update it to show the information you need.

How you do this will depend on your root view controller setup. Is your view controller embedded in an UINavigationController? If so then try this:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if let rootViewController = window?.rootViewController as? UINavigationController {
if let viewController = rootViewController.viewControllers.first as? ViewController {
let url = alert["imgurl"] as? NSString
viewController.loadImage(url as String)
}
}
}

If it is not, then try this:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if let rootViewController = window?.rootViewController as? ViewController {
let url = alert["imgurl"] as? NSString
rootViewController.loadImage(url as String)
}
}

how to call a method in a view controller from Appdelegate in Swift?

You don’t need to call the view controller method in app delegate. Observe foreground event in your controller and call your method from there itself.

Observe for the UIApplicationWillEnterForeground notification in your viewController viewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.yourMethod), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)

Implement this to receive callback when user enters foreground

@objc func yourMethod() {
// Call getDateTimeFromServer()
}

swift ios - How to run function in ViewController from AppDelegate

ViewController().grabData() will create a new instance of the ViewController and call this function. Then.. as the view controller is not in use it will be garbage collected/removed from memory. You need to be calling this method on the actual view controller that is in use. Not a new instance of it.

The best option would be to listen for the UIApplicationDidBecomeActive notification that iOS provides.

NotificationCenter.default.addObserver(
self,
selector: #selector(grabData),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)

make sure that you also remove the observer, this is usually done in a deinit method

deinit() {
NotificationCenter.default.removeObserver(self)
}

How to call viewController's method in appdelegate

This may help you (tested in Swift 4)

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: Notification.Name.UIApplicationDidBecomeActive,
object: nil)
}

}

@objc func applicationDidBecomeActive() {
print("UIApplicationDidBecomeActive")
}

Note: Don't forget to remove observer when your view controller is no longer in use/memory (as this is an application level observer and will be called every time your application becomes active, whether your view controller is active or not.

Here is code to remove observer:

NotificationCenter.default.removeObserver(self,
name: Notification.Name.UIApplicationDidBecomeActive,
object: nil)

Calling view controller method from app delegate

I would try

MainViewController * vc = [[MainViewController alloc]init];
[vc myMethodHere];
[vc release];
  1. Make sure to import your MainViewController in your app delegate .m file
  2. make sure you add "myMethodHere" to your MainViewController .h file

how to access view controller functions in the app delegate in swift 4

Try to add observer in your view controller

NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillTerminate),
name: .UIApplicationWillTerminate,
object: nil)

callback

@objc func applicationWillTerminate() {

self.pauseGame322()

}

Call app delegate method from 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)


Related Topics



Leave a reply



Submit