6

I have a tableview with custom section headers. The view for the section header is defined in the storyboard and wired to an instance variable. Is there a way to request a new instance of the view from the storyboard?

In the past I have done this by having the section header defined in its own xib file and getting a new instance by using

[[NSBundle mainBundle] loadNibNamed:@"TimerViewSectionHeader" owner:self options:nil];
UIView *newHeaderView = self.sectionHeaderView;
Besi
  • 22,579
  • 24
  • 131
  • 223
railwayparade
  • 5,154
  • 1
  • 39
  • 49

6 Answers6

3

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.

3

I am not sure about just views, but the way that I was able to get view controllers out of my storyboard is as follows.

UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"IdentifierName"];

From here, perhaps you might be able to use this similarly to how it was once done with nibs.

Krejko
  • 901
  • 1
  • 9
  • 23
3

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.

agilityvision
  • 7,901
  • 2
  • 29
  • 28
  • +1 from me. I spent hours trying to figure out how to do table header views the Storyboard way because I figured Storyboard was the new and improved way of laying out the UI of my app. Unfortunately using Storyboard fails to deliver in this department and the best solution is to go back to the old way of loading a nib for the view or building the view by code. I really wanted to believe that all the UI of my app could be done in a single Storyboard file but this is definitely not the case. – ozz Jan 12 '12 at 19:55
3

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

Community
  • 1
  • 1
Besi
  • 22,579
  • 24
  • 131
  • 223
  • are you sure you have to call the callbacks yourself - that looks freaky to me – Rhubarb Nov 14 '12 at 18:05
  • nonetheless +1 for the idea of using a UIView as a placeholder. Note you could combine this with Krejko's answer above, and instead of loading the controller from a nib, load it from a storyboard (you can use one storyboard to hold a bunch of miscellaneous views) – Rhubarb Nov 14 '12 at 18:07
  • @Rhubarb I am not 100% certain that you have to do the callbacks yourself but if you embed the view, then you're responsible for calling it, it seems. Please note that in iOS6.0 there is a new mechanism to embed a whole ViewController in an area of your current view controller using an Embed Segue. This might be an option too. – Besi Nov 15 '12 at 09:30
0

I've been able to reuse a view in the storyboard just by connecting a transition from one tableview into the one I want to reuse.

so my tableview that I want to reuse is pointed to twice. It sort of works but the problem I'm running into it setting a variable (using instantiateViewControllerWithIdentifier) in my app delegate to my table view that is getting reused. It seems that if I reuse it, the storyboard is creating 2 instances of my tableview and the one I get with instantiateViewControllerWithIdentifier isn't the one I want.

I'm not really sure if this is the proper way to do it. But I assume many others are doing this somehow. With the custom table cells in storyboard I suspect lots of people want to reuse their views.

Besi
  • 22,579
  • 24
  • 131
  • 223
roocell
  • 2,429
  • 25
  • 28
-1

For example: We want to reuse the view(include subviews) in storyboard shown below.

The target view in storyboard we want to reuse

The best solution I know so far is clip and paste the view related code to the New Singe View file without losing the information.

Detailed steps are as follows

Step 1: Rename the view we want reuse. Just prepare for step 2.

step 1

Step 2: Open storyboard as source code in order to clip the XML code we need

step 2

Step 3、4: Search and clip the code we need

step 3

step 4

Step 4.5(Not needed): Open as Interface Builder to see the view removed

step 4.5

Step 5、6: New XXX.xib and paste the code we clipped just now

step 5

step 6

Step 7: Important. Insert code<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> to XXX.xib source code. Warning: Do this before open it as Interface Builder! Otherwise, you will see wrong size and layout waring.

[![step 7][9]][9]

Step 8: New XXX.swift to connect the XXX.xib

[![step 8][10]][10]

Step 9: Add the view anywhere we want

[![step 9][11]][11]

I get warning: "You need at least 10 reputation to post more than 8 links." Can you support me to upload the remaining 3 screenshots?