Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code reuse in iOS applications

i am very new to ios development, rather i have just started work on my first app. Now my app has a home button on almost every page and behind that button the same code snippet is called to move to the home screen. This is a lot of duplicate code in every controller that has a home button. And it is just an example. There are many other scenarios like this and programmer still learning to code, i think its bad practice as any change will have to be made separately on every controller.

So my question, what are the best practices in scenarios like this when coding for ios??

like image 849
Khizar Avatar asked Oct 19 '11 12:10

Khizar


2 Answers

One easy thing to do in this situation is to make a UIViewController subclass (MyAppMasterVC, for instance) and define your button as so:

- (IBAction)myCommonButtonAction { // code and such }

In all of your view controllers, inherit from this one instead of UIViewController (a la @interface MyNewViewController : MyAppMasterVC).

like image 195
Ben Mosher Avatar answered Oct 13 '22 15:10

Ben Mosher


The first thing to do is to learn more about OO programming and class hierarchy, and understand how you can make a common base class for all of your similar controllers.

like image 28
Hot Licks Avatar answered Oct 13 '22 17:10

Hot Licks