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??
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
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With