Reusing a storyboard view

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

In the past, I did this by specifying the section header in my own xib file and getting a new instance with

[[NSBundle mainBundle] loadNibNamed:@"TimerViewSectionHeader" owner:self options:nil]; UIView *newHeaderView = self.sectionHeaderView; 
+6
source share
6 answers

I don’t think there is a way to do this. It’s best to put the tableview custom header view in a separate tip and load it the same way as in the sample code when you need to use it.

+3
source

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

I really like working with fold-down files, and I was impressed with how quickly I can create a working interface. However, as soon as you need to reuse views, it makes sense to add them to a separate thread along with a subclass of UIViewController .

You can then put the shared UIView in all places where your reusable view will be used, and add the view using the ViewController:

 [myReusableViewController loadView]; [myReusableViewController viewDidLoad]; // You have to handle view callbacks yourself. [self.myReusableViewPlaceholder addSubview:myResusableViewController.view]; [myReusableViewController viewWillAppear:YES]; 

So, to summarize:

  • Use a storyboard that's great
  • Create the scaffold of your application in the storyboard along with any static representation (for example, about screens, etc.).
  • Create reusable views in a custom subclass of nib + UIViewController and add UIView placeholders to the storyboard.

In another answer, I thought about some of the pros and cons of storyboard

+3
source

The solution I came up with for this is as follows:

I have a table view with several prototype cells that displays complex data. There is a detailed view to a detailed view and view of the transaction process.

This first table view has a search button that displays a new table view with the results. It needs the same functionality as the main table view that pushes it; including segues to details and insights on transaction progress, thus:

In the storyboard, select and copy the main table view. Deselect and paste. Create a push segue from your main table view to your second table view; or where do you want to go from it. Change the second view of the table as you like. IE: If this requires some user interface changes, the problem does not occur.

Create a new viewcontroller class, which is a subclass of the view manager that runs the main table view.

Override the data delegate in your subclass to serve a subset of the required data.

Go back to the storyboard, select your second table controller, and in the Identity Inspector, select your subclass as a custom class.

For this solution to work smoothly, your application really needs to manage the data for views. You can use prepareforsegue to transfer data from the first table view to the second, but I found the application data model more flexible from different points of view.

If you have buttons that click on subviews through segue, your subclass will need to override functions that click through segues with identifiers. NB Segues must have unique identifiers, if you identify them at all.

To understand this, it took a lot of trial and error, but as soon as you understand the concept, this is a relatively smooth solution that is quite adaptable and not so poorly implemented.

+3
source

I'm not sure about just views, but the way I was able to get view controllers from my storyboard is as follows.

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

From here, you might be able to use this in the same way you did it with nibs.

+3
source

I was able to reuse the view in the storyboard by simply linking the transition from one table view to the one I want to reuse.

therefore, my tabular view, which I want to reuse, is specified twice. This is kind of work, but the problem I encountered setting the variable (using instantiateViewControllerWithIdentifier) ​​in my application delta is for my table view, which is being reused. It seems that if I reuse it, the storyboard creates 2 instances of my table view, and the one I get with instantiateViewControllerWithIdentifier is not the one I need.

I am not sure if this is the right way to do this. But I believe that many others do this anyway. Using custom table cells in a storyboard, I suspect that many people want to reuse their views.

0
source

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

The target view in storyboard we want to reuse

The best solution I know today is to trim and paste the code associated with the view into the New Singe View file without losing information.

Detailed steps are as follows

Step 1: Rename the view we want to reuse. Just get ready for step 2.

step 1

Step 2: Open the storyboard as source code to crop the XML we need

step 2

Step 3-4: find and crop the code we need

step 3

step 4

Step 4.5 (not required): open as Interface Builder to see the remote view

step 4.5

Step 5-6: New XXX.xib and paste the code we just cut

step 5

step 6

Step 7: Important. Paste the <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> code into the XXX.xib source code. Warning: do this before opening it as Interface Builder! Otherwise, you will see the wrong size and layout.

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

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

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

Step 9: Add A View Anywhere We Want

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

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

-1
source

Source: https://habr.com/ru/post/904040/


All Articles