1

I have a view with a navigation bar, with buttons, and actions associated with them for example. Is there a way to package up parts of the view and controller so I can drag and drop it into other views along with its code? And of course maintain the code in a single place where the changes reflect in all pages using it.

Is this possible by creating a framework and super classes so it can shared across the project and even other projects? Not sure about packaging the UI view components themselves too. Any experience or direction would be greatly appreciated!

TruMan1
  • 33,665
  • 59
  • 184
  • 335

1 Answers1

0

This is a pet peeve of mine. You are asking how to modularize and reuse parts of a view CONTROLLER, not a view.

UIView and UIViewController are in totally different families of objects. Referring to a view controller as a view causes no end of confusion.

Please go back and edit your subject and question body to use the correct terms.

EDIT:

Probably the cleanest way to do this is to combine view controllers using container views and embed segues. You make each group of components it's own view controller. Then when you want to host a view controller's content in another view controller, you follow these steps:

  • Create an instance of your component view controller in your storyboard and configure it as desired.

  • Drag a container view onto your hosting view controller and position it where you want it. (Remember to set constraints so it actually gets placed there at runtime.)

  • Control-drag from your container view onto the component view controller that you want to add as a module.

When you do this the component view controller becomes a building block that you can simply drop into another view controller and it functions independently. It works very well, and does what you want, where the code and views behave as a unit.

That's pretty much it. If you want to connect the hosting view controller and the component view controller together you can add code in the hosting view controller's prepareForSegue method that sets the hosting view controller as a delegate of the component view controller, and you can save a pointer to the component view controller so you can send it messages later if you need to.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I updated but I'm actually talking about packaging up parts of the view and the controller. I don't just need the UI, but along with its code actions and overridden functions so it looks AND behaves the same. – TruMan1 Apr 28 '15 at 23:46