0

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?

2
  • 1
    Add a deinitto your PrimaryViewCoordinator. Is it being called sooner than expected? Commented Jul 13, 2023 at 18:02
  • Oh wow yeah it was! Storing the coordinators before calling the start() functions fixed the issue. Thank you so much! This issue has been killing me for a week now. Commented Jul 13, 2023 at 18:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.