Multiple entry points to a storyboard - ios

I need to make a set of decisions in the AppDelegate on launch - depending on the outcome of those decisions I need to go to specific parts of the storyboard.
So my question is - WITHOUT using any nav or tab controllers, how do I go to a specific part of a storyboard?
OR
Is the only supported option having multiple storyboards - for each of the 'outcomes' and then loading those as required?
Thanks

Give each ViewController a unique identifier in the storyboard.
Then in the appDelegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"PUT_UNIQUE_ID_HERE"];
//DO WHAT YOU WANT WITH YOUR VIEWCONTROLLER
//Example:Set it as the root view of the app window...

Give each of your ViewControllers a separate ID, and then instantiate the required one with:
UIViewController *initialVC = [storyboard instantiateViewControllerWithIdentifier:#"<identifier>"];

Related

Proper Way to Open a New Storyboard through UITabBarController

We are working on splitting our main storyboard into smaller ones so that it makes source control merging easier. Any ideas on what the right approach is to load a new storyboard from a UITabBar?
Here's what we have so far in our custom subclassed UITabBarController:
UITabBarItem *cardsTabItem = [self.tabBar.items objectAtIndex:kTabBarIndexCards];
cardsTabItem.image = [[UIImage imageNamed:#"navCardsOff"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
cardsTabItem.selectedImage = [[UIImage imageNamed:#"navCardsOn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
cardsTabItem.imageInsets = UIEdgeInsetsMake(-5, 0, 5, 0);
cardsTabItem.titlePositionAdjustment = UIOffsetMake(0, -5);
I've done the same thing before, but with a UITabBarController. In that case we had a storyboard for each of the tab buttons, even though one of the storyboards only had 1 view controller in it. I think wether you're using a UITabBarController or responding to the tab bar delegate the answer is the same. For each button clicked make the determination of which storyboard the view controller you want to load is in:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"leftButtonStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc = [storyboard instantiateInitialViewController];
//or
UIStoryboard *otherVC = [storyboard instantiateViewControllerWithIdentifier:#"CameraViewController"];
Then you can present it, push it, or whatever.
In my case since I was using a UITabBarController this was all done during initialization of the controller for all the different buttons.
It will most likely come in handy to by default name all of the different view controller your using in your storyboard (the storyboard id), I usually name them after the viewController class so I don't have to remember what I called it.
I would also recommend that you avoid using the self.storyboard property when trying to instantiate another view controller because you might end up with a situation where a controller is shared between tabs. Being explicit with which storyboard you're loading a controller from can help with readability and avoidance of bugs.
Edit - a more concrete example:
What you need to do is set the viewControllers property of your UITabViewController, I do this in its init method. For example
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UIViewController *one = [mainStoryboard instantiateViewControllerWithIdentifier:#"VC1"];
UIViewController *two = [mainStoryboard instantiateViewControllerWithIdentifier:#"VC2"];
self.viewControllers = #[one,two];
}
return self;
}
You can use this technique if your writing it in code itself or if you're using a storyboard. Beware that if you have other view controllers already hooked up via the storyboard you'll loose then unless you instantiate them there as well. You can also use the setViewControllers: animated: method as well.
The code for creating the custom tab bar items (the buttons at the bottom) should probably go within the individual view controllers and be assigned to its tabBarItem property. The UITabBarController will use that property to create the correctly styled button. If you don't provide the property you get the default buttons starting from 1.

Storyboard UITabBaar, initiate two different views from button

I've created a storyboard that has a UITabbarController all is working well but now I want to add some logic that determines which viewcontroller a particular tabbar button will display.
Example... if a customer has a valid subscription display viewcontroller one, if no subscription display viewcontroller two.
Is this possible using storyboards, I've looked at UITabBarDelegate and prepareForSegue but struggling to piece this together?
Are there any examples of how to do this sort of thing using StoryBoards?
Many thanks
You can set it like this:
if(hasSubscription)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController1* subsection = [storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
ViewController2* subsection1 = [storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[(UITabBarController*)self.window.rootViewController setViewControllers:[NSArray arrayWithObjects:subsection,subsection1, nil]];
}
If you want to add rootviewController according to the subscription then above answer given by soryngod is good one.
But if you want to open viewControllers after rootviewcontroller loaded, then at tabBarButton press perform following code:-
before this code, add yours viewControllerONE and viewControllerTWO to the rootViewController by segues as shown: . And give each segue an identifier in AttributeInspector, example "one" for viewControllerONE and "two" for viewControllerTWO.
then at tabBarButton action do the following:-
if(subscription)
[self performSegueWithIdentifier:#"one" sender:self];
else
[self performSegueWithIdentifier:#"two" sender:self];

Identifying a UIStoryboard

How do you identify a UIStoryboard?
The class has methods which create and instantiate but I don't see an #property with something like name. E.g.
Getting a Storyboard Object
+ storyboardWithName:bundle:
Instantiating Storyboard View Controllers
– instantiateInitialViewController
– instantiateViewControllerWithIdentifier:
Any suggestions?
====
UPDATE
I was hoping for something like self.storyboard.name or [self.storyboard description], e.g.:
NSLog(#"This Storyboard is: %#", self.storyboard.name);
Perhaps it's not meant to be.
You can identify a storyboard by its name in the project navigator:
You can identify a view controller from a storyboard by setting its Storyboard ID in the identity inspector in interface builder:
Once you have these, then you can access them through your code:
UIStoryboard *iPhoneStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController *firstViewController = [iPhoneStoryboard instantiateViewControllerWithIdentifier:#"FirstViewController"];

Splitting Storyboard at UITabBar level

I'm searching for a solution to split Storyboard at UITabBar level.
I have an app with 5 tab and i want to manage every single tab with a different storyboard.
The structure would be a simple minimal storyboard with the tab bar controller and 5 bigger storyboards with every tab viewcontrollers (and segues) which must inherit the tab bar.
Does everyone ever splitted the storyboard like this ? Any clue?
Thanks
I think it could be possible, this should go in "viewDidLoad" of your custom UITabBarController...
NSMutableArray *controllersArray = [[NSMutableArray alloc] init];
// Load the initial UIViewController from every Storyboard
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard1" bundle:nil];
UIViewController *first = [sb instantiateViewControllerWithIdentifier:#"firstViewController"];
[controllersArray addObject:first];
// Repeat the process with the rest
...
// Set your controllers
self.viewControllers = [controllersArray copy];
Remember to set an Storyboard ID for every main UIViewController, in the example is 'firstViewController'.
we used RBStoryboardLink from: http://robsprogramknowledge.blogspot.co.il/search/label/UIStoryboard
to solve this issue

How do I change "initwithNibName" in storyboard?

I want to change below code with storyboard with Xcode 4.2.
UIViewController * example = [[ExampleViewController alloc] initWithNibName:#"ExampleViewController" bundle:nil];
Now ExampleViewController.xib file exist.
but I want to make it with storyboard.
please help me.
(I'm not good at English. Sorry)
The UIStoryboard class is your friend:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"mystoryboard"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"ExampleViewController"];
If it is still in its own xib file, then you don't change anything.
If you've moved everything into a storyboard, then you wouldn't often need to do this as you'd link between view controllers using segues.
If neither of the above are true, i.e. your view controller is on the storyboard but no segue connects to it, then you want UIStoryboard's instantiateViewControllerWithIdentifier: method described in the documentation. You have to set the identifier in the storyboard for this to work.

Resources