aryaxt
aryaxt

Reputation: 77636

iPhone - SubClassing UIToolbar the right way?

  1. I created a new Class named CustomToolbar
  2. Then i created an empty nib, added a toolbar to it, and set the toolbar class to "CustomToolbar".

What is the proper way of initializing CustomToolbar In code so that my class uses the nib file?

I already have written the code to do that but i know it's not the correct way and it has a leak.

@interface CustomToolbar : UIToolbar {
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *button;
@end

@implementation CustomToolbar
- (id)initWithDelegate
{
     NSArray *objects = [[NSBundle mainBundle]
      loadNibNamed:@"CustomToolbar" 
      owner:nil 
      options:nil];
    if (self = (CustomToolbar*) [objects objectAtIndex:0])
    {
        //do some work here
    }
    return self;
}
@end

Upvotes: 0

Views: 1155

Answers (1)

aegzorz
aegzorz

Reputation: 2219

The NIB will call initWithCoder: on your custom class, like this:

- (id)initWithCoder:(NSDecoder*)aDecoder {
    self = [super initWithCoder:aDecoder];
    if( self ) {
        // Do something
    }
    return self;
}

If you really want to load it the way you do now, you need to retain the object returned from loadNibNamed.

Upvotes: 1

Related Questions