Reusing code in iOS apps

I am very new to ios development; rather, I just started work on my first application. Now my application has a home button on almost every page, and behind this button the same code fragment is called up as to go to the main screen. This is a lot of duplicate code on every controller that has a home button. And this is just an example. There are many other scenarios like this, and the programmer is still learning how to code, I think its a bad practice, since any changes must be made separately on each controller.

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

+5
source share
4 answers

In this situation, you can easily subclass UIViewController( MyAppMasterVC) and define your button as follows:

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

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

+4
source

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

+2
source

iOS . - , . , , , , , . , , . , , . , .

+2
source

This issue is a language / platform agnostic. The term used by many is "DRY", the abbreviation "Do not repeat yourself."

Here is a SO search . This should help you with typical problems and needs, so you can better determine if you can, when you want, and how to approach this problem.

+1
source

All Articles