Reputation: 555
I am writing a code in which I have a view controller with certain tabs. Upon the dismissal of a an alert, I want open 2nd tab automatically. For this purpose I posted a local notification when the alert this dismissed. The observer for this notification is in home view controller, when this observer is called, 2nd tab of the view controller is selected. Here is my code for a better understanding:
vc.dismiss(animated: true, completion: {
// vc is the view controller in which my custom alert is shown
NotificationCenter.default.post(name: NSNotification.Name.OpenConsumerTab,
object: ConsumerHomeTab.Stats.rawValue)
})
And home view controller: (question related code)
NotificationCenter.default.addObserver(self,
selector: #selector(onOpenConsumerTabNotificationRecieved(notification:)),
name: Notification.Name.OpenConsumerTab,
object: nil)
@objc public func onOpenConsumerTabNotificationRecieved(notification: Notification) {
if (notification.object as! Int == ConsumerHomeTab.Stats.rawValue)
{
selectedIndex = 1
}
}
This is opening the tab but the output I am obtaining is this: (getting an extra black bar)
Why is this happening? Maybe it is showing up a completely new home view controller (with tab bar) but what is the reason? Correct me if I am wrong as I am newbie to iOS.
Things I have tried:
Get the root view controller (home view controller) and select its tab but the output was same.
Using pop view controller but it opens up first tab (index 0) but my requirement is to open the second tab (index 1)
Upvotes: 2
Views: 2123
Reputation: 186
I think there is no need of using Notification center too, u can write this code when you are dismissing that VC
to dismiss your alert
alert.dismissWithClickedButtonIndex(-1, animated: true)
to change View-Controller:
self.tabBarController?.selectedIndex = selectedTabIndex
// selectedTabIndex is the index of ViewController which you want to select
Upvotes: 3
Reputation: 148
Please use tabbarController instance to set selected index as, may be you not using tabbar controller instance to set index :
if let tababarController = self.window!.rootViewController as! UITabBarController? { tababarController.selectedIndex = 1
}
Upvotes: 0