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 :
ViewController
? If yes, then how?Thanks in advance for your help
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With