UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Coordinator Pattern + Tab Bar Controller + Modal Image Picker

Forums > Swift

Hi,

I have been trying to implement the coordinator pattern in my app, and it uses a UITabBarController. One of the tabs is an image picker that would present modally, which I would display like so:

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if self.viewControllers?.firstIndex(of: viewController) == 1 {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            self.present(imagePicker, animated: true)
            return false
        }

        return true
    }

Moving over to the coordinator pattern, this is my tab bar controller so far

    lazy var homeCoordinator = HomeCoordinator(navigationController: UINavigationController(), viewModel: self.viewModel)
    lazy var imagePickerCoordinator = ImagePickerCoordinator(navigationController: UINavigationController(), viewModel: self.viewModel)

    override func viewDidLoad() {
        super.viewDidLoad()

        homeCoordinator.start()
        imagePickerCoordinator.start()
        self.viewControllers = [homeCoordinator.navigationController, imagePickerCoordinator.navigationController]
        self.delegate = self
        setupComponents()
    }

and in the imagePickers are:

class ImagePickerCoordinator: Coordinator {

    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    let viewModel: ViewModel

    init(navigationController: UINavigationController, viewModel: ViewModel) {
        self.navigationController = navigationController
        self.viewModel = viewModel
    }

    func start() {
        let imagePickerController = ImagePickerController()
        imagePickerController.tabBarItem.image = #imageLiteral(resourceName: "plus")
        imagePickerController.coordinator = self

        //shows up on the tab bar
        //self.navigationController.pushViewController(imagePickerController, animated: true)

        //doesn't show up on the tab bar
        //self.navigationController.present(imagePickerController, animated: true)
    }
}

class ImagePickerController: UIViewController, UIImagePickerControllerDelegate   {
    weak var coordinator: ImagePickerCoordinator?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    }
}

However I cannot get the image picker show modally. Push displays the button, but doesn't work, and present doesn't even show the tab bar button.

Any help would be much appreciated.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.