-1

In my project you can create a post from a modal view.

When the modal view is dismissed (user presses on save post) I want to switch the tab bar controller to the second tab (post feed screen).

This topic is similar to my problem. The only difference being this is presented from a modal view. I can't figure out how to implement it in my code (tab bar is nil) Switch tab bar programmatically in Swift

I have added 3 images to make this issue clearer

feature to implement

code screenshot

console message

@objc func saveAction(sender: UIButton) {
    print ("> save pressed")
    print(presentingViewController?.tabBarController)
    print(presentingViewController)
    
    presentingViewController?.tabBarController?.selectedIndex = 1
    dismiss(animated: true)
} 

edit: sorry stack overflow doesn't allow me to add images yet

kryscram
  • 3
  • 4

1 Answers1

0

You can do this using delegate pattern. But if you prefer not to add a delegate for this, you can do as shown below;

You can switch the tabbar by changing the selectedIndex property of tabBarController

if let presenter = presentingViewController as? LibraryViewController {
        presenter.tabBarController?.selectedIndex = 1
    }
dismiss(animated: true)

If you are presenting the modal on navigation controller in tabbar, use:

    if let tabBar = presentingViewController as? UITabBarController {
        tabBar.selectedIndex = 1
    }
    dismiss(animated: true)
Jithin
  • 913
  • 5
  • 6
  • Hello Jithin ! Thank you for the quick answer. But I have tried this. The problem seems to be that in the modal view the tab bar returns nil. I have edited the post with better detail and my code. – kryscram Aug 15 '20 at 14:32
  • can you show how you are presenting the modal view controller? Are you using `self.present` or `self.navigationController.present`? – Jithin Aug 15 '20 at 14:40
  • In Tab[0] of the tab bar controller i have this method a method "self.navigationController?.present(view, animated: true, completion: nil)" – kryscram Aug 15 '20 at 14:47
  • Ive tried this but it appears that the code within the if statement is not called – kryscram Aug 15 '20 at 14:53
  • I've updated the answer again since you are presenting on the navigation controller. No need to use the `if let`. – Jithin Aug 15 '20 at 14:54
  • I think i'll have to find another way of doing this Instagram like navigation... It doesn't seem to be as obvious as it looks haha Ive been on it for 2 weeks (ARGH) – kryscram Aug 15 '20 at 15:00
  • it didn't work? how many tabs do you have 2 or 3? If its 2, use `presentingViewController?.tabBarController?.selectedIndex = 1` – Jithin Aug 15 '20 at 15:04
  • I have 2 tabs. Ive tried putting `selectedIndex =1` too. But when the saveButton is pressed, the view dismisses properly but the tab bar stays on the `selectedIndex = 0` – kryscram Aug 15 '20 at 15:06
  • can you pls check the value of `presentingViewController' and `presentingViewController?.tabBarController`? – Jithin Aug 15 '20 at 15:11
  • I put `print(presentingViewController?.tabBarController)` in the code and in the console returned nil. I think its because of the fact that its a modal view and not a view controller that has been pushed onto the navigation controller – kryscram Aug 15 '20 at 15:15
  • was `presentingViewController` printed as `UITabBarController` ? – Jithin Aug 15 '20 at 15:22
  • No it presentingViewController was printed as `Optional()` – kryscram Aug 15 '20 at 15:25
  • I've updated the answer to check for tabbar controller. If this still doesn't work, try casting it to `MainTabBarController` instead of `UITabBarController` – Jithin Aug 15 '20 at 15:28