0

I really need your help. I know that what my question contains not so good UI practices, but I need to do it...

In my app I have TabBarController with four items. The first item is "Channel" item, the third item is "Search" item. So, when users selecta "Search" item, I need show him "Channel" and searching something in ChannelViewController. I want that "Channel" and "Search" items have only one view controller - ChannelViewController.

I can present ChannelViewController, but then TabBar navigation isn't showing. I've written this code in SearchViewController:

    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

        let channelViewController = self.storyboard!.instantiateViewControllerWithIdentifier("channelViewController") as! ChannelViewController
        self.presentViewController(channelViewController, animated: false, completion: nil)

    }

Can I do this without SearchViewController? Can I just add or show/hide when I select "Search" item?

Is it possible do something like this:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {     
    if tabBar.items?.indexOf(item) == 2 {
        // and here some code to show tabBar.items with index 0
    }
}

? Any help is greatly appreciated!

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
Roman Romanenko
  • 736
  • 9
  • 24

1 Answers1

1

Try this in one of your controllers:

self.tabBarController?.selectedIndex = 0
pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • 1
    Try to put that line inside viewDidAppear of your third tab view controller. Have you properly connected your tabBar viewController with items in storyboard? – pedrouan Sep 01 '16 at 12:00
  • Thank you, man! When I put it inside viewDidAppear it's worked! – Roman Romanenko Sep 01 '16 at 12:12
  • Well, I'm glad to help. Now, if you want, try to tweak it a bit, and put that line inside viewWillAppear/viewDidLoad, so it doesn't blink. Because your user doesn't have to see blank screen, while you move him onto the first tab. I am not sure if this will work, but you can try. – pedrouan Sep 01 '16 at 12:16
  • Can you tell me how I should add UISearchController? Should I add UISearchController as subviews on first tab or it should be in third tab? – Roman Romanenko Sep 01 '16 at 12:18
  • You should add it inside the controller, your would like to be the target controller for displaying results. Anyway, this appears as a new question, so the best would be to create new one. – pedrouan Sep 01 '16 at 12:19
  • Ok, I understand! But can you give me on more tip about blink? I tried put that line in viewWillAppear/viewDidLoad as you say, but when I clicked quickly I see the blank screen. – Roman Romanenko Sep 01 '16 at 13:03
  • It's because if put inside viewDidAppear, it first appears in the original controller, and then it jumps to the first tab. That's why I suggested to put it inside viewWillAppear. – pedrouan Sep 01 '16 at 13:08
  • Hello, @pedrouan, can you give the answer on this question, please [link](http://stackoverflow.com/questions/39487531/how-to-get-rid-of-blink-between-switching-tab-bar-item) – Roman Romanenko Sep 14 '16 at 12:19