There are three types of users in an iOS app which I am working on these days, the user types are listed below:
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:
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:
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..
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.
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.