how to get currently viewing viewcontroller? - ios

When a push notification arrives, i need to get currently viewing ViewController from the method didReceiveRemoteNotification: of AppDelegate. I have been using both TabbarController and NavigationController in my App. When i try to get it from navigation stack (top item), i get my CustomNavigationController. But i need to get the viewing ViewController (might be an item in tabbar). Would you please help. Thanks in advance.

Just make a variable in viewDidLoad or viewDidAppear for tabBar and set it in appdelegate.
You can check using this current viewController being viewed.

Related

Calling Functions From AppDelegate in swift3

I'm using OneSignal to process notifications in my app. This is initiated in AppDelegate.swift where there is a function that handles received apps:
let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in
print("Received Notification: \(notification!.payload.body)")
}
I then have a TabViewController with 5 tabs and each tab's navigationItem has a UINavigationBarButton that sends you to a messages view. I want to change the badge number in that button whenever a notification comes in to show that the user is getting new messages. I have a function inside every viewController (each of the 5 tabs) that will update this.
The problem is that the AppDelegate needs to call this function updateBadgeNumber and I don't know how to do this. Plus, some of the tabs may not even have been initialised yet. Does anyone know how to call functions in ViewControllers from the AppDelegate?
Thank you.
EDIT:
Found a solution thanks to #paulvs below. He linked me to an answer where I found this: Find Top View Controller in Swift
The answer depends on whether your UITabBarController is the root view controller of your app.
If your UITabBarController is the root view controller, you likely have a reference to it in your app delegate and can use UITabBarController's selectedViewController property to get the current view controller and then set the badge value. (If you don't have a reference, you can probably cast window.rootViewController to UITabBarController to access it.)
If it's not, you can use code like this to find the currently visible view controller. (Then to access the UITabBarController, you can probably cast parentViewController to a UITabBarController, and from there access the other view controllers and set their badge values, too.)

Is there anyway I can find out the viewcontroller being shown on the screen in the app without using viewWillAppear/viewWillDisappear

I am trying to find the page the user is viewing at a given point of time. Is this possible without making any call from viewWillAppear or viewDidLoad. I have tried keywindow but it works only for navigation controller and it doesnt work as expected for tabBarController or a containerView.
Thank you.

iOS: Navigate to certain row after didReceiveLocalNotification in SplitViewController (Swift)

Could someone please help give me some pointers on how to tackle this situation:
My application is a fairly straight forward Master Detail application using a UISplitViewController.
I've setup UILocalNotification and create each one with a userInfo containing a reference.
When I get the notification and swipe it takes me to back into the app and I can see the userInfo in didReceiveLocalNotification.
My question is how do I now use that information to take the user straight to that particular Item in the Detail View Controller?
Say the Detail View Controller is already open with another Item loaded, I can't seem to get popToRootViewController to work.
This is my first time working with UISplitViewControllers and it's giving me some headaches.
Appreciate any help.
Thanks
There should be a segue setup from Master to Detail. Then you need to call performSegueWithIdentifier from didReceiveLocalNotification which will open Detail View. Set correct UserInfo in prepareForSegue method which will be called before Detail View opens. You may check out a simple Master Detail Sample done by me.

Run method every time view is shown - iOS

I understand that you can use ViewDidLoad to run some code when a view is loaded. However that only happens once. How can I run a method every single time that view is shown. So for example: let says you are currently in ViewController A and you press a UIButton to go to ViewController B. Then you press a button to go back to ViewController A, how would you then re-run the ViewDidLoad code??
I hope my question makes sense. In essence I want to re-run a small method every single time the user is on a particular ViewController.
Thanks for your time, Dan.
viewWillAppear:
Notifies the view controller that its view is about to be added to a
view hierarchy.
or
viewDidAppear:
Notifies the view controller that its view was added to a view
hierarchy.
This answer didn’t help. I found myself in the same situation and the solution is simple.
Create a method with codes you want to be executed every time your view controllerA shows up.
Place your method under viewDidAppear()
Make sure that the modal type of your viewcontrollerB is set to fullscreen (fullscreen removes viewcontrollerA from the stack)
Under viewcontrollerB viewDidLoad()
Set its background other color than clear. White works for me
Dismiss from your viewControllerB and “voila!”
Create an AbstractController that every view controller inherits from that controller, and you override the viewDidLoad method and make it do whatever you want at every view opened.

iOS Check if Coming from Particular View Controller

I'm working on an iPhone app where I move through push through several view controllers. On the last on I [self.navigationController popToRootViewControllerAnimated:YES]
I want to ask is there a way to detect that I just came from ViewController7 when i return to the ViewController1?
The reason being i'd like the viewDidAppear to behave in a certain way if it is.
Otherwise is it possible to rerun the ViewDidLoad? (I'm presuming its not).
Thanks.
You could have your viewController1 conform to the UINavigationControllerDelegate protocol and become the UINavigationController's delegate. Then in navigationController:willShowViewController:animated: check if the controller to be shown is viewController1, check your UINavigationController's visibleViewController and set some variable in viewController1. Then in viewDidAppear you can animate appropriately.
I'd use the delegation design pattern to set a protocol method to send information back regarding what view controller you are in.

Resources