ManuQiao ManuQiao - 7 days ago 5
Objective-C Question

Can I interrupt a method of a child in parent class?

I declared

ParentClass
.

Then
ChildClass: ParentClass


Is it possible to interrupt the overridden method in ChildClass after 'do something in ParentClass' so that 'do something in ChildClass' will not be executed?

//ParentClass.m

- (void)method
{
/*do something in ParentClass*/
/*try interrupting [child method]*/
}


//ChildClass.m

- (void)method
{
[super method];
/*do something in ChildClass*/
}

Answer

No, you can't control a method up the call chain like this. Nor should a class have this sort of knowledge about its subclasses.

You'll have to find some way for ClassB to check the condition itself, whether via the return value from -[ClassA method], or by calling another method first, or checking it directly.