Iphone: writing some methods in a separate file

here's a quick question, I have a couple of class controller classes and there are other classes. I found myself creating some methods in all of my implementation files that do the same.

I would like to write some of my methods in a separate file, and import this file into my view controllers. so I can run these methods from my current view controller,

If this method requires a change, I will not need to overwrite it in all my implementation files.

Let me wipe only one thing. I do not want to create a new object! and create an instance. I really want to be able to run this method the same way I did: [self doSomething];

Please help to understand how this should be done.

i.e. I have 3 view controllers, each of which has the following method:

-(void)doSomething:(id)sender{
//doing something
}

I just want this doSomething method to be defined in one place, and I could access it from my view managers by running: [self doSomething];

Is it possible?

Thanks in advance.

+3
source share
1 answer

You want to do this by subclassing. First, create a class in which you want all your common methods, like a subclass of UIViewController, to call it BaseViewController. For example, this will be in your header file:

@interface BaseViewController: UIViewController 
{
}
-(void)doSomething:(id)sender;
@end

And then the corresponding definition in your .m file.

, UIViewController, , BaseViewController, UIViewController, :

@interface MyViewController: BaseViewController
{
}
@end
+2

Source: https://habr.com/ru/post/1745998/


All Articles