I want to display the "Menu View Controller" when a local notification was opened. I use a segment control in the "Menu View Controller", which should be set to selectedSegmentIndex = 0, when the view was opened through local notifications. I am able to open the View Controller but I do not know how to address the segment Control from app delegate? This is the code I use:
// open the menu view controller when notification is pressed
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let scene = UIApplication.shared.connectedScenes.first
if let sceneDelegate = scene?.delegate as? SceneDelegate {
if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController {
tabController.selectedIndex = 0 // index for menu view conntroller
}
}
// set selected index of the segment control equal to zero
completionHandler()
}
I am not tried your answer , but try this
steps :
initially you need to get the viewcontrollers of tabbarcontroller, like this
if let getMenuVC = tabController.viewControllers.first, let getMenu = getMenuVC as? yourMenuController{
}
then you need to get the current class as your menuviewcontroller as like
if let getMenu = getMenuVC as? yourMenuController{
}
finally you need to upadte the segment selected index of your current class as like
if let getMenu = getMenuVC as? yourMenuController{
getMenu.yourSegmentedControlName.selectedSegmentIndex = 0
getMenu.yourSegmentedControlName.sendActions(for: UIControl.Event.valueChanged)
}
final code as :
if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController {
tabController.selectedIndex = 0 // index for menu view conntroller
if let getMenuVC = tabController.viewControllers?.first, let getMenu = getMenuVC as? yourMenuController{
getMenu.yourSegmentedControlName.selectedSegmentIndex = 0
getMenu.yourSegmentedControlName.sendActions(for: UIControl.Event.valueChanged)
}
}