[Solved] iOS how present Modal ViewController from Uiview


When you need a communication between UIView instance and UIViewController, there are a few known iOS concepts, which you should adhere to. As you have figured out that UIView cannot really present a new controller (missing either presetViewController:animated:completion methods or navigationController property, which are both present in UIViewController).

Views are supposed to be the most reusable parts of your code, so you must think of a way to design your views to be completely blind to where they are at. They usually only know about user interaction.

So first, what you must do is refactor your view.

  1. If your UIView is supposed to be a UIControl (has some kind of target selectors), you need to use add target in your controller to get callback from view interaction.
  2. You can use delegate pattern as used in UITableView or UICollectionView, which is designed as a protocol.
  3. You can use gesture recognizers added to a view (UITapGestureRecognizer for example), so the controller knows about user interaction.

You can even mix and match those architectural patterns.

But you should really look into iOS programming basics, to understand this better.

In addition the first error I see in your code is that you create a generic UIViewController, when you should really be creating custom subclasses of it, defined in Storyboard and separate subclass of UIViewController.

The second error I see is that your UIView responds do tableView:didSelectRowAtIndexPath: method, which should in fact never happen. All this code must be moved back to one UIViewController subclass.

solved iOS how present Modal ViewController from Uiview