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.