reuse view from storyboard

I dont' think there is a way to do that. Best bet is to put the tableview custom header view in a separate nib and load it like you did in your code sample whenever you need to use it.


I tried to do the same thing and ran into the same problem.

I like to work with storyboards a lot and was impressed how fast I could create a working UI. However, as soon as you need to re-use views it makes a lot of sense to put those into a separate nib along with its UIViewController subclass.

You can then place a generic UIView in all the places where your re-used view should go and add the view using your ViewController:

[myReusableViewController loadView];
[myReusableViewController viewDidLoad]; // You have to handle view callbacks yourself.

[self.myReusableViewPlaceholder addSubview:myResusableViewController.view];
[myReusableViewController viewWillAppear:YES];

So to sum it up:

  • Use storyboard, it's great
  • Create the scaffold of your application in the storyboard, along with any static view (like About screens etc.)
  • Create re-used views in a custom nib + UIViewController subclass and add UIView placeholders in your storyboard.

In another answer I thought about some Pros and Cons of Storyboard


The solution I've come up with for this is as follows:

I have a tableview with multiple prototype cells that displays complex data. There is a segue to a detail view, and a transaction process view.

This first tableview has a search button that displays a new tableview with the results. It needs the same functionality as the main tableview that pushes it; including segues to the detail and transaction progress views so:

On storyboard, select and copy your main tableview. Deselect and paste. Create a push segue from your main tableview to your 2nd tableview; or from where ever you want to navigate to it from. Modify the 2nd tableview as you like. IE: If it requires some UI changes no problem.

Create a new viewcontroller class that is a subclass of the viewcontroller running the main tableview.

Override the data delegate in your subclass to serve up the subset of data you want.

Back in the storyboard, select your 2nd tableview controller and in the identity inspector select your subclass as the custom class.

For this solution to work smoothly, your app really needs to be managing data for the views. You could use prepareforsegue to pass data from 1st tableview to the second, but I've found the app data model far more flexible from numerous points of view.

Unless you have buttons that push to the sub views via segue, your subclass will need to override functions that push via segues with identities. NB Segues must have unique identifiers if you id them at all.

It took a lot of trial and error to figure this out, but once you understand the concept, it's a relatively smooth solution that is quite adaptable and not so bad to implement.