UISplitViewController - rightBarButtonItem on the primary view controller does not work

31 Views Asked by At

I have an IpadCoordinator defined as:

class IpadLayoutCoordinator: BaseCoordinator {

    private let rootViewController: UINavigationController

    private lazy var splitVC: UISplitViewController = {
        let vc = UISplitViewController(style: .doubleColumn)
        vc.minimumPrimaryColumnWidth = 450.0
        vc.maximumPrimaryColumnWidth = 450.0
        return vc
    }()

    init(navController: UINavigationController) {
        self.rootViewController = navController
    }

    override func start() {
        let primaryViewCoordinator = PrimaryViewCoordinator(rootView: splitVC)
        primaryViewCoordinator.start()
        self.parentCoordinator?.store(coordinator: primaryViewCoordinator)

        let secondaryView = UIViewController()
        secondaryView.view.backgroundColor = .blueColor           
        splitVC.setViewController(secondaryView, for: .secondary)
    
        let containerVC = UIViewController()
        containerVC.addChildViewController(splitVC, toView: containerVC.view)
        rootViewController.setViewControllers([containerVC], animated: true)
    }

}

My primary view controller is set through the PrimaryViewCoordinator as follows:

class PrimaryViewCoordinator: BaseCoordinator {
    
    private let rootViewController: UISplitViewController
    
    private lazy var primaryVC: PrimaryViewController = {
        let vc = PrimaryViewController.loadFromNib()
    
        /* This is the button not working */
        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.goToAddVC))
        vc.navigationItem.rightBarButtonItem = addButton
    
        return vc
    }()

    init(rootView: UISplitViewController) {
        self.rootViewController = rootView
    }

    override func start() {
        rootViewController.setViewController(primaryVC, for: .primary)
    }

    @objc private func goToAddVC() {
        /* This is the action not working */
        let addItemCoordinator = AddItemCoordinator(rootView: rootViewController)
        addItemCoordinator.start()
        self.store(coordinator: addItemCoordinator)
    }

}

The AddItemCoordinator just presents the AddItemViewController over the entire view. The problem is that the code inside of the @objc selector never executes. The action never executes. I can edit the bar button type and visuals but the action will not execute.

If I add a rightBarButtonItem the same way for the secondary view controller, it works just fine. It's only the primary view controller that has this issue.

I even tried adding the rightBarButtonItem in the start() function in case it was losing its reference to self but that didn't work either.

Does anyone have an idea on how to fix this bug?

0

There are 0 best solutions below