Connecting to Superclass Properties with Storyboards

48 views Asked by At

I am converitng a project to Swift with Storyboards. The old version was Objective-C and did not use Storyboards. The original version has a number of ViewControllers that are subclasses of a CoreDataFetchedResultsViewController. In the CoreFetchedResultsViewController class, I have a property for a UITableView that is used in a number of methods. The declaration of the UITableView in the CoreFetchedResultsController superclass is below.

@interface CoreDataFetchedResultsViewController : UIViewController <NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate>  
@property (nonatomic, strong) UITableView *storyTableView;

In the ViewControllers that subclassed the CoreFetchedResultsViewController, I created a tableView and set it to the storyTableView property of the CoreFetchedResultsViewController in the viewDidLoadMethod.

@interface FLOViewController : CoreDataFetchedResultsViewController <FLODataHandlerDelegate, UIActionSheetDelegate> 

viewDidLoad() from the FLOViewController class

- (void)viewDidLoad  
{  
    [super viewDidLoad];  

        self.storyTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 44, 320.0f, 436.0f) style: UITableViewStylePlain];  
        self.storyTableView.backgroundColor = [UIColor clearColor];  
        self.storyTableView.rowHeight = 81;  
        self.storyTableView.separatorColor = [UIColor blackColor];  
        [self.view addSubview: self.storyTableView];  
        self.storyTableView.delegate = self;  
        self.storyTableView.dataSource = self;  
} 

With the tableView connected to the CoreDataFetchedResultsViewController property the tableView did not crash the app.

My trouble occurs when I am using Storyboards. Since I place the tableView on my FLOViewController in the Storyboard, I am not able to connect it to the storyTableView property in the superclass CoreDataFetchedResultsViewController.

What's the best way to connect a Storyboard element to a superclass property? Do you create an outlet in the subclass and then set that property to the superclass property?

class FLOViewController  
{  
@IBOutlet var floViewControllerTableView: UITableView!  

    override func viewDidLoad()  
    {  
        super.viewDidLoad()  
        self.storyTableView = self.floViewControllerTableView  
     }  
} 

This appears to work but is seems wrong to me. It seems as if copying the object is not the best idea. Any help would be appreciated.

Take care,

Jon

1

There are 1 answers

0
jonthornham On

Creating an IBOutlet in the superclass is what works best. This way you can connect the tableView directly to this property.

Take care,

Jon