tags:

views:

29

answers:

2

I have a class named myClass. This class has TouchesMoved and TouchesBegan defined.

This class is used by 3 other classes. But I need to add more code to each of these classes, to the same TouchesMoved and TouchesBegan methods, without overriding the code that is already on its class. I mean, if I add a TouchesBegan method to one of the 3 classes, it will not use the class definition for the method. I would like to be able to, instead, add code, instead of replace.

Example:

MyClass
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self doStuff1];
}


Object1 based on MyClass
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self doStuff2];
}

when touchesBegan of Object1 runs, it will call doStuff2 and also doStuff1, instead of just doStuff2.

Is that possible?

thanks

+2  A: 

Can't you just call [super touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event] in Object1's touchesBegan?

Nick
+1  A: 

I would say it depends on the solution design. If the idea is that your base class handles those events and in between gives the chance to the derived classes to do their own things, then you can declare a method like onTouchesBegan, call it from your base class' touchesBegan implementation, and implement it in your derived classes as needed. This is less error prone than relying on the derived class to call the base. Also the base will be in control of the order of things.

fhj
thanks, that's it.
Digital Robot