Search code examples
iosswiftsegue

View Controller disappearing after Segue


I have a customer tab controller that has a custom icon that when a user clicks a popup menu comes up with 3 choices. When I click the first option it should take me to a new view controller, however when I click it the view controller only appears for a second before disappearing again. I'm not sure why but here is my code for the customer tab bar:

import UIKit
import PopMenu

class TabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.title == "for custom action" {
            let manager = PopMenuManager.default

            let action1 = PopMenuDefaultAction(title: "Scan Barcode", didSelect: { action in

                self.performSegue(withIdentifier: "showScanBarcode", sender: nil)
                print("\(String(describing: action.title)) is tapped")
            })

            let action2 = PopMenuDefaultAction(title: "Action 2", didSelect: { action in

                print("\(String(describing: action.title)) is tapped")
            })

            let action3 = PopMenuDefaultAction(title: "Action 3", image: UIImage(named: "wine"), didSelect: { action in
                print("\(String(describing: action.title)) is tapped")
            })

            manager.addAction(action1)
            manager.addAction(action2)
            manager.addAction(action3)

            manager.present()


            return false
        }
        return true
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "mySegue" {

            let controller = segue.destination as! myViewController

            controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
            controller.navigationItem.leftItemsSupplementBackButton = true

        }
    }

}

Here is an image showing the flow. User clicks the camera button, then a popup menu appears and when the user clicks on an option I want to take them to a new view controller (not connected to tab bar controller). I setup the first link to go to a new view controller, and it shows for a few seconds then disappears.

enter image description here


Solution

  • Thats a problem with PopMenue.

    PopMenuManager is displaying a UIViewController on the topmost view controller and calls dismiss() after a selection. Dismiss goes recursive through all controller. By the time he does that, is your new view controller the topmost and receives the dismiss. It might help to do the segue in a separate thread. (Along with a short delay for testing maybe)

        let action1 = PopMenuDefaultAction(title: "Scan Barcode", didSelect: { action in
    
            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "showScanBarcode", sender: nil)
            }
            print("\(String(describing: action.title)) is tapped")
        })