4

I am finding that when I initialize a new child class, I'm getting an object of the parent class returned.

Parent:

- (id)init
{  
    self = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"EditController"];        
    return self;
}

Child:

- (id)init
{
    NSLog(@"New child");
    self = [super init];
    return self;
}

If I alloc and init a child, it displays New child but gives me back an object of the parent's class. Calling methods declared only in the child cause a crash.

Could someone explain how the initialization process is working here, and how I can create a child object?

Ben Flynn
  • 17,294
  • 18
  • 92
  • 133
  • 1
    My workaround will be to use a standard XIB instead of a storyboard, but I'm still curious about this. – Ben Flynn Apr 07 '12 at 18:51
  • 1
    Yup, newer question but one with an actual answer! – Ben Flynn Jun 17 '15 at 18:04
  • Sorry, I did flag the question when I saw the other one (and thought it was older, as it got more attention somehow...), and only then noticed the date on both (at the very least, your's is not marked as "duplicate" but as "has an answer elsewhere", which correct and non derogatory for you)... – Rick77 Jun 26 '15 at 08:40
  • No worries at all. Once it got flagged it asked if the answers to the other question answered my question, so I selected yes. Happy to see duplicates reduced on SO! – Ben Flynn Jun 26 '15 at 18:56

1 Answers1

0

I'd bet the crash is caused by the parent's init returning an autoreleased instance of EditController. Either way, why don't you just set the class type to that of the child directly in the storyboard?

Lvsti
  • 1,475
  • 14
  • 15
  • I have multiple child classes inheriting the same basic elements, so I think I'd need basically identical looking copies in the Storyboard, no? The XIB works fine for me. I'm not sure how it could be an autorelease issue since the pool shouldn't have the chance to clear by the time the child init is called, and also if I assign an init'ed with Storyboard parent class, it is retained until I explicitly release it... – Ben Flynn Apr 22 '12 at 23:36
  • Yeah in that case using a XIB would make more sense. As for the memory issue, in the code you posted you overwrite `self` with the **autoreleased** instance obtained from `UIStoryboard`, which is a programming error to say the least (goes against naming conventions). – Lvsti Apr 24 '12 at 08:42