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 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()
}
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)
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)
Xcode; How to call function in AppDelegate from another ViewController
Try something along those lines:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")
EDIT:
'Today Extension' and App live in a different contexts so it is not possible.
iOS - Calling App Delegate method from ViewController
You can access the delegate like this:
MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate];
Replace MainClass with the name of your application class.
Then, provided you have a property for the other view controller, you can call something like:
[appDelegate.viewController someMethod];
Calling method in current view controller from App Delegate in iOS
To do the refresh of the view do not call viewWillAppear
if the view is already displayed. What you want to do is the following:
When ConnectionDidFinishLoading
method is triggered post a notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:nil];
In your viewController
observe for this notification. You do it by adding this code to your init or viewDidLoad
method
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshView:) name:@"refreshView" object:nil];
Now implement -(void)refreshView:(NSNotification *) notification
method in your viewController
to manage your view to your liking.
Related Topics
How to Disable Back Swipe Gesture in Uinavigationcontroller on iOS 7
How to Set the Height of Tableheaderview (Uitableview) with Autolayout
Autolayout: Removefromsuperview/Removeconstraints Throws Exception and Crashes Hard
Programmatically Navigate to Another View Controller/Scene
Uisearchbar Increases Navigation Bar Height in iOS 11
iOS - Calling App Delegate Method from Viewcontroller
Autolayout with Hidden Uiviews
iOS 11 Navigation Bar Height Customizing
Long Press Gesture on Uicollectionviewcell
Swift - Problems with Corner Radius and Drop Shadow
Make a Uibutton Programmatically in Swift
iOS Perform Action After Period of Inactivity (No User Interaction)
How to Convert My Device Token (Nsdata) into an Nsstring