Create multiple storyboard iPhone/ipad - ios

Create multiple storyboard iPhone/ipad,use the same viewcontroller,but iPhone storyboad can run,iPad error message(can run on ios simulator,but can't not run on my ipadair).
this is my code:
NSBundle *resource = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:#"LibraryResource" ofType:#"bundle"]];
[resource load];
UIUserInterfaceIdiom userIdiom = [[UIDevice currentDevice] userInterfaceIdiom];
if (userIdiom == UIUserInterfaceIdiomPad) {
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:resource];
LibraryController* LibraryController = [mainStoryboard instantiateViewControllerWithIdentifier:#"LibraryController"];
[self presentViewController:LibraryController animated:YES completion:nil];
} else if (userIdiom == UIUserInterfaceIdiomPhone) {
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:resource];
LibraryController* LibraryController = [mainStoryboard instantiateViewControllerWithIdentifier:#"LibraryController"];
[self presentViewController:LibraryController animated:YES completion:nil];
}
error message:
2014-03-04 14:24:25.515 AddLibrary[311:60b] Cannot find executable for CFBundle/CFPlugIn 0x157e0aad0 </var/mobile/Applications/184A78F9-1488-4C34-AD5C-10170067ACEA/AddLibrary.app/Library9898API Resource.bundle> (not loaded)
2014-03-04 14:24:25.522 AddLibrary[311:60b] Unknown class LibraryController in Interface Builder file.

Make sure you have included your file to your target that is your app bundle. Ensure you have checked the target membership.

add your view controller to your build phases.
Clean and rebuild it again. It should work.
If not then reset the simulator then try
Rename your view controller files and class name and then add it your project.

iPadAir must support (64-bit),use the old project not change

Related

iOS - access class method using targets

I have the following code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyViewController" bundle:nil];
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyViewController class])];
When I use targets my problem is [MyViewController class] is returning "MyTarget_2.MyViewController" then my app crash because "'Storyboard (<UIStoryboard: 0x600001e65d80>) doesn't contain a view controller with identifier "
I have two targets:
MyTarget and MyTarget_2 and is only failing with the last one.
I don't understand is why [MyViewController class] is returning
MyTarget.MyViewController. When I start a new project this is not happening, only returns MyViewController . Why is such a difference?
This seems to be related because I'm using swift+objective-c
Calling NSStringFromClass on a Swift Class in Objective-C returns module mangled name
You can get class name like this :
NSString *className = [[NSStringFromClass([MyViewController class]) componentsSeparatedByString:#"."] lastObject];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyViewController" bundle:nil];
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:className];
and make sure that your storyboard id should be MyViewController
Did you give Identifier to your ViewController in storyboard ?
You can give it against Storyboard ID in ViewController's identify Inspector section.
Because ,
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyViewController class])];
method work on the identifier and not on class name
I think you haven't copied that particular storyboard in to your target. You can verify it. Click on target. Then on Build Phase -> Copy bundle resource. Check if storyboard file exist in both targets. And if storyboard is not there click on + button add that file. Clean and run. Hope it help.

How to call different Storyboards in iOS?

I am using two storyboard first one for iphone Main.storyboard and second one is Main_iPad.storyboard and i wants to call separately for iPad and iPhone i am using this code:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
EABaseRootVC *loginController = [storyboard instantiateViewControllerWithIdentifier:#"root2"];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController=navController;
}
else
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EABaseRootVC *loginController = [storyboard instantiateViewControllerWithIdentifier:#"root"];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController=navController;
}
But its always shows iPhone assets for Main.storyboard.
i am already follow this Different storyboards for iPhone and iPad in xcode 6
if you have any other idea so please suggest me.
what i am doing wrong?
To identify Apple devices specifically whether it is iPhone or iPad use below code:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// code for iPad
}else{
// code for iPhone
}
And must add these two keys inside your project's .plist file.
Main storyboard file base name (iPad)
Main storyboard file base name (iPhone)
See image:

Obj-C Change ViewController programmatically (Two storyboards)

First of all, I want to highlight, that this code is working, but bad-looking. I need to change view controller programmatically (can't use segue), so I use this code:
NSString *storyboardName = #"Main";
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:storyboardName bundle:nil];
PurchaseViewController *vc = [storyboard
instantiateViewControllerWithIdentifier:#"PurchaseViewController"];
[controller presentViewController:vc animated:YES completion:nil];
Obvoiusly its working. Unfortunately after create 2nd storyboard (iPad) I was forced to change this code to:
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
/*
...
*/
NSString *storyboardName;
if (IDIOM == IPAD)
storyboardName = #"Main_iPad";
else
storyboardName = #"Main";
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:storyboardName bundle:nil];
PurchaseViewController *vc = [storyboard
instantiateViewControllerWithIdentifier:#"PurchaseViewController"];
[controller presentViewController:vc animated:YES completion:nil];
It must be better solution than this, but I didn't found one. Do you know alternative(better) solution?
Keep your iPad Storyboard name same as iPhone just append '~ipad' before '.storyboard' and add 'Main storyboard file base name (iPad)' in info.plist and put Storyboard name as value. Then use below code to get storyboard name according to device.
NSBundle *bundle = [NSBundle mainBundle];
NSString *storyboardName = [bundle objectForInfoDictionaryKey:#"UIMainStoryboardFile"];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
Hope this will help.

Multiple storyboards not loading localization

I have an application that has a few different Storyboards, and is using Base Internationalization for localizing into French. The Main.storyboard that has its .strings file with translations loads with French just fine. However, when I instantiate a new storyboard, and present it, it remains in English. I was simply doing this to load the storyboard before:
UIStoryboard *upcomingStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
/// Code to present initial view controller.
This just loads the English storyboard. I then tried following the instructions from this site, which changed my code to:
NSString *language = #"Base";
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *preferred = [[mainBundle preferredLocalizations] objectAtIndex:0];
if ([[mainBundle localizations] containsObject:preferred]) {
language = preferred;
}
NSBundle *bundle = [NSBundle bundleWithPath:[mainBundle pathForResource:language
ofType:#"lproj"]];
UIStoryboard *upcomingStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
All this did was cause the app to crash when loading the storyboard, which is probably because there no actual storyboard file in the fr.lproj, just a .strings file. Has anyone had any success with this?
I'm using multiple storyboards in my localised app. None of them are set as the "Main Interface" file. They are all loaded in code depending on where the user needs to be in the app.
Essentially I have a login storyboard that walks the user through logging in and an "app" storyboard for the content of the app.
The project uses base internationalisation (with English as the development language) and strings files for all the translations (nibs, storyboards and code).
They are loaded through a method like this...
- (void)showStoryboardWithName:(NSString *)storyboardName transition:(UIViewAnimationOptions)transition
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:[NSBundle mainBundle]];
UIViewController *controller = [storyboard instantiateInitialViewController];
[self showViewController:controller withTransition:transition];
}
I have 15ish languages localised in the app and they all work with this.

Adding a second storyboard causes "could not find storyboard" error

I started by building 2 versions of my app, on for 3.5 inch screens and one for 4 inch with only the storyboard being different. I brought the 3.5 storyboard into the 4 inch project and used the following code in my appDelegate.m to have the program run the appropriate storyboard.
UIViewController *vc;
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568.0f)
{
NSLog(#"IPHONE IS WORKING");
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"Main.storyboard" bundle:nil];
vc = [storybord instantiateInitialViewController];
}
else
{
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"MainFour.storyboard" bundle:nil];
vc = [storybord instantiateInitialViewController];
}
}
else
{
UIStoryboard *storybord = [UIStoryboard storyboardWithName:#"iPad" bundle:nil];
vc = [storybord instantiateInitialViewController];
}
[_window setRootViewController:vc];
[_window makeKeyAndVisible];
When I use this, I get the error:
"Could not find a storyboard named 'Main.storyboard' in bundle
NSBundle"
The error happens even if I try and use the storyboard that was not imported, the one that runs if the above code is missing. So I am assuming that the error lies in the name of the storyboard. In the project navigator, the storyboard is named "Main.storyboard" but calling that name does not find it. What am I doing wrong? Thanks.
Drop the .storyboard extension from the name you specify.
From the reference:
storyboardWithName:bundle:
Creates and returns a storyboard object for the specified storyboard resource file.
+ (UIStoryboard *)storyboardWithName:(NSString *)name
bundle:(NSBundle *)storyboardBundleOrNil
Parameters
name The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.

Resources