Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storyboard - Creating Multiple Views in storyboard for the same ViewController

I am designing an iPad application using storyboard. I have a ViewController showing a view. My problem is, that when an admin sees that screen, he should see a certain view whereas when a user sees that screen, he should see another view. I thought I should create two views for the same screen and load the appropriate one depending on who has logged it. However, while I could do this in XIB files in prior iOS versions, the storyboard does not allow me to create a view outside the ViewController so i cannot design multiple views.

My questions are :

  1. Can we design two views in storyboard for the same ViewController? If yes, then how?
  2. I need to see the two Views side by side so I can make changes to them separately. Having them as sub-views of the main view will load both the views at run-time. I want to be able to load only one of the views depending on who has logged in.

Thanks in advance for your help

like image 421
inforeqd Avatar asked Feb 20 '12 05:02

inforeqd


People also ask

Can we use multiple storyboards in one application?

Apple introduced them in iOS 9 and macOS 10.11. They do exactly what I needed. They allow you to break a storyboard up into multiple, smaller storyboards. A storyboard reference ties multiple storyboards together, creating one, large, composite storyboard.


1 Answers

You can show the view of one storyboard controller in another, but it's tricky. I had a tab view controller in storyboard where one of the views had a NavBar with a segmented control on it that determined which view of two views appeared in the controller. I'll call this the "primary" controller. What I did was on the "primary" controller, inside the main View, I added two more views on top of each other that fit between the navbar and the tabbar and connected IBOutlets to them.

@property (retain, nonatomic) IBOutlet UIView *leftView;
@property (retain, nonatomic) IBOutlet UIView *rightView;

When the left segment of the segmented control was pressed, the rightView was hidden (setHidden:TRUE) and the leftView was unhidden. Vice-versa for the right segment.

To show the view from another ViewController inside one of the above Views, in the "primary" view controller I created an IBOutlet for each secondary ViewController

@property (retain, nonatomic) IBOutlet CustomViewController1 *leftViewController;
@property (retain, nonatomic) IBOutlet CustomViewController2 *rightViewController;

The layouts of the secondary view controllers have to match the "primary" controller in terms of the appearance of navbar, statusbar, and tabbar items

I then had to instantiate them from the storyboard manually in ViewDidLoad on the "primary" view controller.

self.leftViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomViewControllerOne"];
self.rightViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomViewControllerTwo"];

Where "CustomViewControllerOne" and "CustomViewControllerTwo" are the "identifier" field values of the controllers in storyboard, which I had to enter.

Again in ViewDidLoad on the "primary" controller I added the Controller views as subViews of the ones I was hiding and unhiding based on the segment control

[self.leftView addSubview:leftViewController.view];
[self.rightView addSubview:rightViewController.view];

I found that if I tried to add them as subviews of the main View without creating the two view containers (leftView and rightView) the secondary view controllers appeared offset in the "primary" controller.

So when the user pressed the left segment, the view from CustomViewController1 appeared and when they pressed the right segment the view from CustomViewController2 appeared.

like image 148
nh32rg Avatar answered Jun 19 '23 10:06

nh32rg