开发者

Reusing data entry methods across views

I have written code for scrolling my table view even when keyboard hides it from entering data, using the notification center and the keyboardDidShow and keyboardDidHide methods.

The problem is that I have almost 8 views in my app where I need to enter some data.

开发者_开发百科

Should I write the whole code in every single .m file, or is there any other easy way I could do it?


You could write some kind of BaseTableViewController which handles all the keyboard notifications.

Then let all the other TableViewControllers inherit from this base controller.


Either you define that method in your application delegate file or create a separate class file which contains the method and you can call it whenever it required.

myMethod.h file

    @interface myMethod : NSObject 
    {
    }
    - (void) callMyMethod;

myMethod.m file

    - (void) callMyMethod
    {
         // your code
    }

In your view, call this method....

   myMethod *objMyMethod = [[myMethod alloc] init];
   [objMyMethod callMyMethod];


The DRY (Don't Repeat Yourself) principal would lead to creating one set of code to handle the input, not many copies that do the same thing.

The principal of decoupling would lead to a separate class for the code.

A separate class would also allow easier Unit Test to be written.


This sounds like a perfect use-case for a category.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜