volperossa
volperossa

Reputation: 1431

Objective C : Custom class in identity inspector creates a new object of that class?

In a storyboard when I add a new view (for example a TableView) I can select a class in the "Custom class" field in the identity inspector.

If I understand the rule of this class, I expect this class "answer" to messages sent to my tableview (i.e. this class is my table viewcontroller) and when I run my project it seems to do what I want.

My question is: To do this, I expected my Xcode automatically instantiates an object of my controller class and "link" this object to my GUI in storyboard.

However, I expected that if I override the init method of my controller class with

-(id) init
{
  self=[super init];
  NSLog(@"object controller created automatically");
  return self;
}

I have the string in output when is created my controller object. Instead, I have no output. Why is this happenig and what is wrong with the code?

Upvotes: 1

Views: 141

Answers (2)

John
John

Reputation: 535

UIView set up by storyboard never called init. Instead, you should use - (void)awakeFromNib in which your outlet has been ready to use.

- (void)awakeFromNib {
    [super awakeFromNib];
    NSLog(@"object controller created automatically");
}

From awakeFromNib documentation:

Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized using their initWithCoder: method. All objects that do not conform to the NSCoding protocol are initialized using their init method.

Upvotes: 2

MasterRazer
MasterRazer

Reputation: 1377

If I understand you question you want a message to be printed whenever your viewController is initialised.

Why dont you write the code in the viewDidLoad?

Like:

In your YourControllerClass.m

-(void)viewDidLoad {

[super viewDidLoad];

NSLog(@"Controller created");

}

Now set the class of the controller in the storyboard to YourControllerClassand the message should be printed whenever your controller is created.

Cheers

P.s.: If you still need help or got a question, please write a comment.

Upvotes: 1

Related Questions