0

There are three types of users in an iOS app which I am working on these days, the user types are listed below:

  • Standard User
  • Team Leader
  • Board Member

As the functionalities for the users can be different, I think it would make sense to have three Storyboards in the app, each containing its respective flow for the user who is currently logged in. So, the Story Boards will be:

  • Standard User Story Board
  • Team Leader Story Board
  • Board Member Story Board

So far, so good!

Now, I have a TasksViewController which is responsible for displaying tasks specific to user currently logged in, this View Controller is created in XIB. TasksViewController offers a common functionality and it makes some logical sense to me that I should be able to use TasksViewController in all the Story Boards.

The questions are:

  • Is this a right thing to do?
  • If it is a right thing to do, is it possible to do so?
  • If its possible, how to implement it?
3
  • Pretty sure what you're asking is if you can see the design in your XIB embedded in all three storyboards. Nearly certain that this is not possible. Commented Apr 28, 2015 at 14:34
  • @SteveMadsen, gotcha! Thats what I am asking for. Not sure if what I am asking is something to ask for or not as a Storyboard feature, but, it sounds logical to me, at least. Commented Apr 28, 2015 at 14:39
  • I agree that it could be quite useful. You should file an enhancement or new feature request Radar, and then, if you're attending WWDC, maybe drop by the labs and talk to an Xcode engineer about it. Commented Apr 28, 2015 at 14:43

2 Answers 2

1
  • Is this a right thing to do?

It depends on your global project. Keep in mind that what you suggest is possible but can make the code hard to understand/debug. Indeed, imagine you push your controller by instantiating it manually from XIB, how to handle segue from this controller (assuming the segue must have a different behaviour depending on the user type)? Switch the segue depending on the user type? Ugly if too frequent..

  • Is it possible to do so? How to implement it?

Yes of course. For example, create a storyboard dedicated to shared components and instantiate controllers on-the-fly when needed (snippet written in Swift, methods are the same in Objective-C).

let sharedStoryboard: UIStoryboard = UIStoryboard(name: "SharedStoryboard", bundle: NSBundle.mainBundle())
let sharedController: UIViewController = sharedStoryboard.instantiateViewControllerWithIdentifier("ControllerIdentifier") as! UIViewController
self.navigationController?.presentViewController(sharedController, animated: true, completion: nil)

This will instantiate a controller contained in your shared storyboard and present it to the user. This is the old way but when dealing with several storyboards, this is the best solution.

  • So what to do ?

If you just have a few controllers, I'd suggest you replicate it for each user - it will also be easier in the future if you need to add user-type specific features on a controller. If a big part of your app is shared, use the way presented above.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Presenting TasksViewController with the way you suggested make sense and would definitely work, however, TasksViewController will not appear in the Storyboard Design Interface within Xcode. I am just wondering if there is a way that TasksViewController can be shown in more than one Storyboards.
1

Yes you can do that. And I think it is good to reuse the same ViewController. The other way would be to use Storyboard to hold all reusable view controllers and reuse the ViewControllers via identifier.

Here is the solution with xib:

NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"TaskController"
                                                      owner:self
                                                    options:nil];

TaskController *tasks = (TaskController *)[nibViews objectAtIndex:0];

// Do the stuff you want here

For completeness sake, here is reusing view controller from storyboard:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"SomeStoryboard" bundle:[NSBundle mainBundle]];
TaskController *nextOpportunity = [sb instantiateViewControllerWithIdentifier:@"YourIdentifier"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.