Proper Way to Open a New Storyboard through UITabBarController - ios

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.

Related

UIButton doesn't work inside UIPopoverController

I have a UIViewController with some buttons that works fine when I add as subview, but when I present this viewcontroller in UIPopoverController none of the buttons trigger actions. Someone knows that problem ?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"iPhoneStoryboard" bundle:nil];
menuViewController = (MenuListViewController *)[storyboard instantiateViewControllerWithIdentifier:#"menuviewcontroller"];
Buttons inside viewcontroller works if I use:
[self.view menuViewController.view];
But I want to use
popover = [[UIPopoverController alloc]initWithContentViewController:menuViewController];
One thing to look at when you can't interact with UI elements, especially after frame resizes, is to check that your elements are not outside of their parents bounds, check my answer to this question

After Pushing View Controller Load a View From Storyboard

I am trying to have a different view controller appear as you change the device orientation and for that I am using a UINavigation Controller. When I call for the other view controller to be pushed [self.navigationController pushViewController:graphView animated:YES]; It makes the transition but the screen is black and does not load this new controller "graphView"'s view which is white with text. I have done graphView = [[GraphView alloc] init]; for the new controller but i do not have any storyboard connections made from the current view to the graph view nor do I have anything in my new view controller other than:
- (void)viewDidLoad {
//[super viewDidLoad];
NSLog(#"loaded");
}
Is there an extra step since this is a new view controller to load the view from this view controller that i have on my storyboard? Also the log does work meaning that is corrent.
Instead of initializing your 'graphView' like:
graphView = [[GraphView alloc] init];
You'll need to do this:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
graphView = [storyboard instantiateViewControllerWithIdentifier:#"YourViewControllerId"];
**And make sure you set your ViewController's storyboard identifier in the storyboard:
Select your ViewController
Click the Identity inspector
Set the Storyboard ID ('YourViewControllerId' above)
Then, calling pushViewController:animated: as you were should present your ViewController appropriately.

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

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

Multiple entry points to a storyboard

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

Resources