Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Using inheritance and calling methods in Child Class

I'm setting up inheritance.

I have a parent class that has a method that calls a block. Once the block returns, I need it to call an over-written method in the child class.

When I setup breakpoints, upon failure, the block is calling the method in the parent class. What am I doing wrong? Thanks.

Parent Class

.h

@interface BaseClass : UITableViewController
-(void)userModelUpdated
-(void)updateModel
@end

.m

@implementation BaseClass

-(void)userModelUpdated
{} //this is left blank intentionally as a hook


-(void)updateModel
{
   //Here is s Block 
    [EndPoint updateUserModel:self.userModel successBlock:^{
    //Do Something
    } errorBlock:^(NSError *error, NSArray *errorArray) {    
       [self userModelUpdated];  // I want to call the method in the child class
      // but when I setup the break points, it calls the method in the parent class
    }
}

Child Class

.h

@interface childClass : BaseClass

.m

@implementation ChildClass

-(void)viewWillAppear:(BOOL)animated
{
    [self updateModel];
}

-(void)userModelUpdated
{
  // update UILabels Here    
}
like image 751
user1107173 Avatar asked Apr 24 '26 10:04

user1107173


2 Answers

If self is an instance of ChildClass when updateModel is invoked, then the ChildClass's implementation of -userModelUpdated will be executed.

If it isn't, then it is because you probably have an instance of BaseClass or you have a misspelling.

Add this to all methods:

NSLog(@"%s", __PRETTY_FUNCTION__);

That'll log exactly what is going on at every step of the way.

like image 89
bbum Avatar answered Apr 25 '26 23:04

bbum


It sounds like your code that is creating an instance of this class family is wrong, and is creating a base class object instead of a child class object. Post the code that creates the object and invokes the method on it.

like image 42
Duncan C Avatar answered Apr 26 '26 01:04

Duncan C