Identifying a UIStoryboard - ios

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

Related

Not able to include controller in Xcode?

I am not able to include the particular controller inside my code but other controllers are included, I included the controller as header but I am not able to include it inside. I attach my code here, I am not able to include IndexPageTableViewController as favoritesViewController?
#import "IndexPageTableViewController.h"
#import "FavoritesViewController.h"
-(void) navigateToFavorites
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone"
bundle: nil];
if(mainStoryboard)
{
FavoritesViewController *favorites = (FavoritesViewController *) [mainStoryboard instantiateViewControllerWithIdentifier:#"FavoritesViewController"];
[[SlideNavigationController sharedInstance ] popToRootAndSwitchToViewController:favorites withSlideOutAnimation:NO andCompletion:^{
[self initTableView];
}];
}
}
As you mentioned in comment that you are getting signal sigabrt error that means you may not set FavoritesViewController as identifier in interface builder. you can set Storyboard ID under identity under Identity Inspector. Custom class and storyboard id both are different thing. I think FavoritesViewController is your class that you have set from identity inspector. If you want to instantiate storyboard then you have to set storyboard id and then you can use that id to instantiate view controller.
Second thing make sure that your storyboard's name is correct and your view controller is within this storyboard. And you have make proper setup if you are using multiple storyboard.

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.

iOS switch view in a navigation controller from the app delegate

I'm trying to switch to another view in my navigation controller when the application didRecieveRemoteNotification is triggered.
Here is my current code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ConvViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"chatConversation"];
[(UINavigationController*)self.window.rootViewController pushViewController:ivc animated:NO];
For some reason i get (lldb) error on the second line.
Any ideas what I'm doing wrong?
Edit
I'm using storyboard segue identifier: chatConv
to my Custom Class: ConvViewController and my custom class has a Storyboard ID: chatConversation
Here is my Storyboard:
chatConversation should be a ViewController in MainStoryboard.storyboard. In MainStoryboard.storyboard, select your chatConversation ViewController. In the Identities panel, make sure your custom class is set to ChatViewController.
instantiateViewControllerWithIdentifier takes a view controller identifier not a segue identifier. So something like:
[storyboard instantiateViewControllerWithIdentifier:#"convViewController"];

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.

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