2

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)

enter image description here

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)

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
kinza
  • 535
  • 11
  • 31

2 Answers2

3

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
Nilomi Shah
  • 186
  • 7
  • Still the same output :( – kinza Jan 31 '18 at 05:12
  • But Why are you dismissing whole ViewController just dismiss the alert and then write this code. – Nilomi Shah Jan 31 '18 at 05:30
  • According to me its happening as you are dismissing the ViewController of tab-bar Controller , dont do that, as tab-bar Controller automatically manages the memory. – Nilomi Shah Jan 31 '18 at 05:34
  • Actually the alert I am showing is on separate VC. When alert gets dismissed it's respective VC should also get dismissed. Otherwise it will just show a black screen. – kinza Jan 31 '18 at 05:46
0

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
}
Vinod N
  • 148
  • 4