0

I want to define a method in a class A. The method should be over-writable for any i̶n̶s̶t̶a̶n̶c̶e̶ subclass of the class A. The instances of subclasses will be stored in a NSArray. I tried to use the delegate. But it didn't work. I checked a few articles posted online such as How do I create delegates in Objective-C? and http://www.tutorialspoint.com/ios/ios_delegates.htm But they seem not what I can use to achieve my goal. Could anyone who are familiar with this give me a hint please?

For example,

I create a base class called BaseClass for the UICollectionViewCell and inside the BaseClass there is a ActionMethod. Click different collection cell will result in a different action. So I will define a subclass for each cell that inherited from BaseClass to implement different action. Then how to overwrite the ActionMethod for each subclass?

Community
  • 1
  • 1
Alex Lee
  • 161
  • 2
  • 8
  • 2
    Over-writable by what? – dan Jun 24 '16 at 15:12
  • @dan Sorry for the unclear. I mean each instance can have their way to define the method in the class A. – Alex Lee Jun 24 '16 at 15:15
  • Can each of these instances be a different subclass of A? If not, what is deciding which implementation of the method should be used for each instance? You really need a more specific example of what you're trying to achieve for this question to be answerable – dan Jun 24 '16 at 15:20
  • I pretend you are coming from Java? In C you can overwrite ANY method of a subclass just by implementingt in the parent class. You dont need to declare it as @overwrite – Thallius Jun 24 '16 at 15:23
  • @dan does the newly added explanation make sense to you? – Alex Lee Jun 24 '16 at 15:40

3 Answers3

0

You mean overriding, not over-writing methods. In Objective-C, you don't have to do anything other than put an implementation with the exact same method signature in the subclass.

NRitH
  • 13,441
  • 4
  • 41
  • 44
  • How to temporarily define a subclass within a .m file? – Alex Lee Jun 24 '16 at 15:45
  • Do you mean like an anonymous inner class in Java? Objective-C doesn't have anything like that. You can define additional classes in a .m file by declaring `@interface` and `@implementation`s for them. But instead of subclassing, you may want to consider making the action method a *block* that can be set dynamically for each cell, as explained [here](http://goshdarnblocksyntax.com/). – NRitH Jun 24 '16 at 15:48
0

Generally, you define a base class, such as Shape, which has a set of methods common to all shapes, such as, getEnclosingRectangle, getShapeArea, etc. Then you define separate subclasses of shapes, such as oval, polygon, rectangle and triangle. You can even define subclasses of those shapes, such as circle derived from oval. Then you could store all your ovals, rectangles and triangles (for example) in an NSArray, and call the getArea method for each of them. And each would have their particular getArea method defined in their class.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
0

A delegate would work here. Another option is to use a callback block, e.g.

@interface Cell : NSObject

@property (nonatomic, copy) void(^onAction)();

- (void)actionMethod;

@end

@implementation Cell

- (void)actionMethod {
    self.onAction();
}

@end

Used as

Cell *cell1 = [[Cell alloc] init];
cell1.onAction = ^{
    NSLog(@"Action 1");
};

Cell *cell2 = [[Cell alloc] init];
cell2.onAction = ^{
    NSLog(@"Action 2");
};

As you can see, you don't need subclasses, you can just add that code to every cell dynamically.

Sulthan
  • 128,090
  • 22
  • 218
  • 270