Reputation: 1431
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
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 ofUIView
andUIViewController
) are initialized using theirinitWithCoder:
method. All objects that do not conform to theNSCoding
protocol are initialized using theirinit
method.
Upvotes: 2
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 YourControllerClass
and 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