简体   繁体   中英

Using same UIViewController in Multiple Storyboards

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?
  • 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!

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM