Today I started messing with Storyboarding in Xcode and there is one issue that kind of makes me feel stupid:
I have a storyboard with a navigation controller and a table view as its root view. Now I added a bar button item to the navigation bar. As I wanted this button to have a custom background I assigned my custom UIBarButtonItem class to this button by selecting the name of my class in the “custom class” field.
The class itself looks like this:
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
{
self = [super initWithTitle:title style:style target:target action:action];
if (self) {
[self setBackgroundImage:[self buttonBg] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
return self;
}
- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
{
self = [super initWithImage:image style:style target:target action:action];
if (self) {
[self setBackgroundImage:[self buttonBg] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
return self;
}
- (UIImage *)buttonBg
{
return [[UIImage imageNamed:@"navBarButton.png" ] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 6, 15, 6)
resizingMode:UIImageResizingModeTile];
}
I wrote this class assuming to add a button programmatically, of course this works as I can call the initializer by myself. The problem is that I don’t know which initializer is called by the Interface Builder, I even tried to overwrite the basic “init”, but it seems that this one is also not called …
I did a lot of searching but didn’t find the right stuff, although I’m certain that this is basic stuff. So please apologize. 🙂
Interface Builder will always call
initWithCoder:. It makes sense, as the IB / storyboard file is actually an XML-encoded file that has to be decoded.I usually define my own
initializemethod and call that from any overridden init methods.