Question

I have a UIViewController's subclass, we call it MySuperClass , this subclass has a UITableView property which is than initalized programmatically. Now I want to subclass MySuperClass to MySubclass but this time I'd like to design the tableview via Interface Builder rather than programmatically. What I want is something similar to how UIViewController works, if you subclass UIViewController it has its view property already initalized, but when you bring it into IB you can link it to an Interface Builder's UIView item, how do I do this?

the source code of my superclass is something similar to this one:

//interface

#import <UIKit/UIKit.h>

@interface MySuperClass : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;


//implementation


#import "MySuperClass.h"


@implementation MySuperClass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        [self initializeProperties];

    }
    return self;
}

- (void) awakeFromNib{

    [super awakeFromNib];

    [self initializeProperties];

}

- (void) initializeProperties{

    self.tableView = [[UITableView alloc] initWithFrame: self.view.frame style: UITableViewStylePlain];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    UIView *tableHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.bannerView.frame.size.height+kBannerDistance)];

    tableHeaderView.backgroundColor = [UIColor clearColor];

    self.tableView.tableHeaderView = tableHeaderView;


}
Was it helpful?

Solution

Just "redeclare" the @property in your subclass.

#import <UIKit/UIKit.h>
#import "MySuperClass.h"

@interface MySubClass : MySuperClass

@property (nonatomic, strong) IBOutlet UITableView *tableView;

@end

The compiler will be clever enough to understand your are referencing the super class property, and IB will have no problem linking to the subclass's one.

OTHER TIPS

This is probably not be the best solution, but it should get it done.

Define - initFromSubClassWithNibName: bundle:; in MySuperClass.h and implement it like this:

- (id) initFromSubClassWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}

and in MySubClass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
}

This way you can escape MySuperView's implementation of the init method and use UIViewController's implementation. You can take the same approach withawakeFromNib`. This will avoid creating a table view programmatically.

Then you can take GuillaumeA's answer to initialize tableView from the IB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top