How can I share common methods between view controllers?
I have an iPhone app, it has two separate MKMapView components in different views, however, many of the actions I will perform with the two views are the same. How do I write the method开发者_开发问答s once but use them in both view controllers?
An example is I have a button which is disabled in both views until the user performs a certain action, I don't want to write the method to enable this button twice so I want to know what is the best-practice way of sharing methods between views? I'm thinking I could write a class and import that into my view controller.
Thanks
You create create your own view controller class that is subclassing the viewcontroller of choice you are using. You could then put this common method in the parent class so it is available in your viewcontrollers. You'll implement this particular subclass you make in each of your views.
Your other option would be to write a sub-controller that handles the shared functionality and use an instance of it in each of your view controllers.
精彩评论