iOS Change Story Board - ios

My iPad App has two storyBoards - FirstStoryBoard and SecondStoryBoard
I have a single ViewController
I notice that Under Project > Targets > Summary there is an option for specifying the MainStoryBoard and if I change this setting the correct story board loads correctly.
The Question - How can I change the behavior so that I can change between the first and second story board at run time. In other words I want to

First, create storyboard instance:
UIStoryboard *board = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
and then get first view controller from it:
UIViewController *vc = [board instantiateInitialViewController];
add as child view controller:
[self addChildViewController:vc];
or maybe (if you are in AppDelegate.m / application:didFinishLaunchingWithOptions: method)
self.window.rootViewController = vc;

The Question - How can I change the behavior so that I can change
between the first and second story board at run time.
You can't switch the value of the Main Storyboard setting (specified by the UIMainStoryBoard key in your Info.plist) at runtime, so do the next best thing: use a common storyboard or nib file that contains very little, and then instantiate the storyboard that you want using the UIStoryboard class. For example, you could use a simple nib file to create your app delegate, and have the app delegate's -application:willFinishLaunchingWithOptions: load first view controller from the storyboard of your choice.

Related

Navigate between multiple storyboards in iOS App

I wanted to have separate storyboard files for every UIViewcontrollers in my iOS app.
So how can we assign different storyboards for each controllers? Also how do we do navigation between those?
This I am doing to avoid svn conflicts while so many people working on UI.
get a reference to the storyboard...
UIStoryboard *someStoryboard = [UIStoryboard storyboardWithName:#"NameOfYourStoryboard" bundle:nil];
then instantiate either the initial viewcontroller from that storyboard...
UIViewController *initialViewController = [someStoryboard instantiateInitialViewController];
or some other viewcontroller identified by it's storyboard identifier...
UIViewController *someOtherViewControllerFromTheStoryboard = [someStoryboard instantiateViewControllerWithIdentifier:#"SomeViewControllersStoryboardIdentifier"];
after that you can simply push (within a navigation controller) or present the new viewcontroller.
since iOS 9.0 you can even connect storyboards via storyboard references in the storyboard itself:
https://developer.apple.com/library/prerelease/ios/recipes/xcode_help-IB_storyboard/Chapters/AddSBReference.html
Keeping different storyboards for different modules is good approach. You can achieve the navigation between storyboards as follows:-
Suppose you are in A view controller and want to push another view controller named FabIdeaDetailViewController which is present in storyboard named FabIdeas:-
FabIdeaDetailViewController *horizontalListController = (FabIdeaDetailViewController*)[UIViewController instantiateViewControllerWithIdentifier:#"FabIdeaDetailViewController" fromStoryboard:#"FabIdeas"];
[self.navigationController pushViewController:horizontalListController animated:YES];
Now for pushing another view controller named WishlistViewController which is present in storyboard named Wishlist:-
UIViewController *WishlistViewController = [UIViewController instantiateViewControllerWithIdentifier:#"WishlistViewController" fromStoryboard:#"Wishlist"];
[self.navigationController pushViewController:WishlistViewController animated:YES];

Using storyboard only for certain view controllers

I have done a separate project using storyboard.
I need to integrate with an existing big project which doesn't use storyboard.
Is it possible to use storyboard partly (only for some view controllers)? If so how ?
Sure. You can instantiate separate view controllers from storyboard:
Getting a Storyboard Object
+ storyboardWithName:bundle:
Instantiating Storyboard View Controllers
– instantiateInitialViewController
– instantiateViewControllerWithIdentifier:
For ex:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
YourViewController *yourVC = [storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerIdentifier"];
Yes. It is possible. Any view controllers that aren't on the storyboard are presented in exactly the same way they were in the old projected, by instantiating, setting properties (if need be), and pushing.

iOS iPad storyboard is not being selected

It seems that Xcode 5 is not choosing to use my iPad storyboard for the iPad device even though i specified it.
This is how i told Xcode 5 to use iPad storyboard:
I went to project settings under General
Selected Devices: Universal
Then i clicked on iPad
And wrote MainStoryboard-iPad.storyboard in Main Interface
But for some reason even though i make changes to my MainStoryboard-iPad storyboard its not being showed when i try to run it on an iPad.
I only have two storyboards in my project
MainStoryboard-iPad.storyboard
and
MainStoryboard.storyboard
Any ideas what could be wrong here?
Oh by the way, when i selected Universal the first time i got a box asking me something about copying (i never read it that carefully). I just hit Yes. Not sure what that box actually did.
EDIT
Code that runs in my AppDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application {
AppDelegate *app = [[UIApplication sharedApplication] delegate];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"mainStoryBoard"];
UINavigationController *nvc = (UINavigationController*)self.window.rootViewController;
app.window.rootViewController = ivc;
}
I just encountered this a while ago.
These are the steps you need to do:
make sure the "Deployment Info" -> Devices is "Universal"
Create a storyboard for iPad (e.g) MainStoryboard_iPad.storyboard
now go to your Infor.plist
add row for "Main storyboard file base name (iPad)" and use MainStoryboard_iPad as the value of the string
Now you are good to go!
Your code is overriding the default storyboard for the device type. You are grabbing MainStoryboard, instantiating a view controller from it and setting it as the root. This is normally handled by the storyboard itself. It uses the view controller that you have picked as the root. Try removing all of that code to manually set the storyboard.
Check this project on github for an example of storyboard switching without code: https://github.com/danielmackenzie/StoryboardSelection
Xcode project points to each storyboard per device type and the appropriate board is automatically chosen on launch.
You'll need an entry for the iPad in your app's info.plist
Look in your Supporting Files group for the info.plist for your app.
You'll see an entry for the iPhone's storyboard, but not one for the iPad.
The iPhone entry should look like this:
Main storyboard file base name
and it should have the name of your iPhone's storyboard (sans file extension) as the string value for it. Something like this:
iPhone-Storyboard
Add a new entry into the pList and enter this string as the key:
Main storyboard file base name (iPad)
Make sure the data type is string.
You can now select the iPad storyboard that you want from the app's General Settings on your Target.
I JUST ran into this as well and thought you'd like a clear explanation and solution.
Sometimes, Xcode is rather maddening, isn't it? Hope this helps.
// Override point for customization after application launch.
if (<your implementation>) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
YourViewController *yourController = (YourViewController *)[mainStoryboard
instantiateViewControllerWithIdentifier:#"YourViewControllerID"];
self.window.rootViewController = yourController;
}
return YES;
Your logic is not taking into account your device. AFAIK, there is no method that looks at your naming postfix ("-iPad") and automatically selects the right storyboard file.
To fix it, simply replace the call to instantiate your storyboard with some logic to pick the right one based on device.
UIStoryboard* storyboard;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard-iPad" bundle:nil];
} else
{
storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
}

My app requires using instantiateViewControllerWithIdentifier - why?

I have added a Storyboard file to an app that initially had none. For some reason, I could not get my custom UIViewController to display correctly until I added this into didFinishLaunchingWithOptions:
ActivityViewController *viewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:#"ActivityViewController"];
Why do I need to force the use of my storyboard like this? The iOS template projects (Single View, Master-Detail etc) doesn't need this.
Checklist:
Xcode Project Summary→Main Storyboard is set correctly to "MainStoryboard".
Interface Builder→Identity Inspector→Class is correctly set to "ActivityViewController".
Interface Builder→Identity Inspector→Storyboard ID is also set to "ActivityViewController", but this is only because it's needed by instantiateViewControllerWithIdentifier.
You do not need to call instantiateViewControllerWithIdentifier if you set Is Initial View Controller on your "ActivetyViewController" in the storyboard. The initial view controller will have an arrow pointing at it.

Using Multiple Storyboards in iOS

My objective is to create a tabbed application, then the view for each of the tabs are constructed in separate storyboards.
My mainstoryboard is a tab view.
Then I create a secondary storyboard (storyboard#2) with 2 View Controllers. The first view controller (also ticked as initial) have a button, and segue (modal) to 2nd view.
I managed to load the view by subclassing and overriding loadView from storyboard#2.
Here's the simulator output.
When click on the "click me" button, I get a EXC_BAD_ACCESS. The segue does not work, seems like the second storyboard is not being loaded completely.
Has anyone tried to do this before and get it working? There's a youtube video from SkillMaster.net but he does not demonstrate if a segue is working under the secondary storyboard. the video is here: http://youtu.be/D4_twoYvB4M
Thanks for any input and help!
Screenshots:
http://www.box.com/s/njnyzjoqg8pnqofv838m
http://www.box.com/s/8dqygclmp5ic86e47bi5
http://www.box.com/s/k7foe7gpgh2rs3y8gqxd
http://www.box.com/s/rym111x7xqxqao51ruip
These are the best articles I've seen on multiple storyboards.
Storyboard best practices
Linking storyboards with segues
Not only does this guy tell you how to create a new storyboard in code, he
recommends multiple storyboards in practice (more modular code)
discusses when to use xibs vs storyboards (xibs hold views, storboards are based on controllers)
provides a class for linking storyboards with segues on github
Note that this last point is important because the key downside of multiple storyboards is that you can't usually link them with segues, but robs library allows that with a bit of fudging
Also see the discussed here
The OP edited his question to include the answer:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"HelpStoryboard" bundle:nil];
UIViewController* initialHelpView = [storyboard instantiateInitialViewController];
initialHelpView.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:initialHelpView animated:YES];
Of course, where you call this from is meaningful, because you might have 2 storyboards and their view stack in memory. So probably best if you call this code from outside the other storyboard's view controllers.
I've examined the RBStoryboardLink approach suggested by Rhubarb.
This implementation substitutes view controller's properties which looks odd. I believe I've found the way to avoid this. Here is the demo project.
Navigation controllers
Navigation controllers could just set a referenced view controller as a root. Implementation of such view controller may look like this:
#interface ExternNavigationController : UINavigationController
#property (strong, nonatomic) NSString *storyboardName;
#property (strong, nonatomic) NSString *sceneIdentifier;
#end
#implementation ExternNavigationController
- (void)awakeFromNib
{
NSAssert(self.storyboardName, #"storyboardName is required");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:self.storyboardName bundle:nil];
UIViewController *vc = self.sceneIdentifier
? [storyboard instantiateViewControllerWithIdentifier:self.sceneIdentifier]
: [storyboard instantiateInitialViewController];
self.viewControllers = #[vc];
}
#end
View controllers
Problems begin when you want to push a view controller defined in an external storyboard. This is the case when properties are copied. Instead of this, we can implement a custom segue which will substitute a fake destination controller with a real one from external storyboard.
#interface ExternStoryboardSegue : UIStoryboardSegue
#end
#implementation ExternStoryboardSegue
- (id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(ExternViewController *)destination
{
NSAssert(destination.storyboardName, #"storyboardName is required");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:destination.storyboardName bundle:nil];
UIViewController *vc = destination.sceneIdentifier
? [storyboard instantiateViewControllerWithIdentifier:destination.sceneIdentifier]
: [storyboard instantiateInitialViewController];
return [super initWithIdentifier:identifier source:source destination:vc];
}
- (void)perform
{
[[self.sourceViewController navigationController] pushViewController:self.destinationViewController animated:YES];
}
#end
ExternViewController is used as a placeholder and contains required for substitution properties (storyboardName and sceneIdentifier).
#interface ExternViewController : UIViewController
#property (strong, nonatomic) NSString *storyboardName;
#property (strong, nonatomic) NSString *sceneIdentifier;
#end
#implementation ExternViewController
#end
We need to set these properties and custom class for placeholder view controller. And also link view controller with ExternStoryboardSegue.
From one of my XIB files I am navigating into a more complicated part of the GUI, and for this part I am using a Storyboard. So a button in my XIB will navigate to the storyboard. The code I have for this is:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MyStoryboardIdentifier" bundle:nil];
UIViewController* myStoryBoardInitialViewController = [storyboard instantiateInitialViewController];
[self.navigationController pushViewController:myStoryBoardInitialViewController animated:YES];
This will successfully push my StoryBoard onto the view. The code above is called from a buttons "Touch Up Inside" action.
As from Xcode 7(and ported back to iOS8) you can now have storyboard references. It was mentioned in this WWDC 2015 session(it starts talking about them around first hour). Basically all you had to do is select ViewController's which you wish to move to separate storyboard, and click on Editor->Refactor to Storyboard.... Give it a name and voila:
Please note that if you had VC's which are moved to the new storyboard, and are not referenced outside of it(what it should be), you should delete their references from main.storyboard(relax they will remain in newly created storyboard, you are deleting only references to them).
Apple's docs say that you may have multiple storyboards. Unfortunately they don't go into any real detail on how to do that. As you've found out, Interface Builder won't help you, so you'll have to do it in code. It works much like loading XIBs:
[UIStoryboard storyboardWithName:#”MyNewStoryboard” bundle:myBundle]
Having said that, if you don't "want one big/bloated storyboard" as you said in your comment then XIBs really are the way to go. That 'bigness' is the benefit: all the transitions between VCs are laid out in one place. Having multiple storyboards is really so that you can support multiple different and unrelated flows through your app: for example, one storyboard for a complex configuration flow and another one for the main user flow.
Xcode 8.2.1
You can reference a view controller in an external storyboard.
drag a connection from the UITabBarController to the external Storyboard Reference, add it as a "view controllers" relationship. In the main storyboard it shows as "Item/square", but in the external storyboard you should add a UITabBarItem and define the name and image/s for the tab.
Attributes inspector when the Storyboard Reference is selected.
You will also need to give the external controller a "Storyboard ID" in its storyboard (not shown here), and reference it's name in the reference.
I've found that if you have a named segue in a second storyboard that you want to use in a call to performSegueWithIdenitfier: you must set the "Storyboard ID" field in the "Identity" tab on the source ViewController.
For example if you gave a VC called "ViewController1" with a segue called "segue1" to another VC, set the "Storyboard ID" of "ViewController1" to whatever (say "viewC1" or something) and then in ViewController1.m you can use [self performSegueWithIdentifier:#"segue1" sender:self].
Follow below steps to achieve this task:
Step 1 : Search for an Storyboard Reference in components (Refer Screen Shot)
Step 2 : Select new storyboard reference. (Refer Screen Shot)
Step 3: Provide new storyboard name and identifier of initial view controller's identifier (Refer Screen Shot)
Build and Run. It will Work.

Resources