Does anyone have any examples? I've created iOS and Android modules for Titanium but haven't found any examples where people use Storyboard for visuals.
I know you can load nibs in modules using initWithNibName, check this SO answer out.
For storyboards specifically, I think that gets complicated since Storyboards define a whole navigation structure. I think you have two options, break down the specific windows in the storyboard into nib files, and then load those as needed, or open up the storyboard itself using code similar to this:
// You have to get the resource bundle first
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"YourStoryBoards" bundle:resourceBundle];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"yourViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
I have not tried this out, but I think the key is supplying the correct resource bundle, and then opening from a modal.
Related
Apple now wants us to use "scenes" rather than windows and screens to display content in for iPad and iPhone. Now having added the support for scenes I seem to have lost the ability to target iPad or iPhone with Storyboards?
I set my scenes inside plist like this:
This was copied from a new project, as Apple seems to have forgotten to document how to add scenes to an existing app. Another example of Apple not documenting sufficiently!
Now I seem to have lost the ability to use different storyboards for iPad from iPhone.
Whilst I could use the same storyboard for the iPad that I use with the iPhone my app looks better with the dedicated interface I have for the iPad because I use the extra real estate it offers to give a better end user experience. iPhone is fine, the interface is best suited to a small display but looks barren on an iPad.
Help!
Now I seem to have lost the ability to use different storyboards for iPad from iPhone
It's quite simple (and, as you say, not documented). You need two completely separate scene manifest entries in your Info.plist, i.e. UIApplicationSceneManifest and UIApplicationSceneManifest~ipad. They just specify different UISceneStoryboardFile values, and you're all set just as before scenes came along.
There may be a better way to do this, but searching I couldn't see anybody else covering this. Therefore, I'm giving my solution which has been accepted by the App Store here for others to review, use, improve upon, and most importantly, not waste any time on a goose chase looking for a solution that may not exist!
The way I got around this was to add another storyboard and view controller just to handle the decision if it's a iPhone or iPad, and then immediately load up the targeted storyboard. I called this "entry decision". The only issue was when closing the subsequent views you have to ensure that you're doing it correctly or you'll end up back at the "entry decision". And with a blank view, your end user could be stuck. Therefore, to ensure a user can never really be stuck I put buttons in that view so they can manually navigate should it show up if there's a change by Apple further down the line that we don't know about. Best to cover it now.
Step 1, create an new storyboard, "entry decision":
Because I'm a nice person I explain to the user it's an error and apologise. I also give them two buttons, iPad and iPhone. In theory, if all goes well, the end user will never see this, but at no cost to us it's better to cover this possibility.
Step 2, as soon as the view controller is loading get it to move to the right storyboards we actually want.
-(void)viewDidLoad {
// Need to decide which storyboard is suitable for this particular method.
// Loading from class: https://stackoverflow.com/questions/9896406/how-can-i-load-storyboard-programmatically-from-class
if ( [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad )
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
}
- (IBAction)pickediPhone:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
- (IBAction)pickediPad:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
There's nothing really special about this as it is a mix of detecting which device and then loading the right storyboards. And for full disclosure, I include a reference back to the original code I used for loading those storyboards.
The additional two methods are just the button presses, shown here for completeness.
Step 3, update the scene configuration inside plist to target Entry Decision instead of Main.
You're targeting a different storyboard, hence this needs updated.
Step 4, returning back to your main screen.
In my app, I have a welcome screen, and then table views or other views depending on which it is. Some types work find without any issues, but as I'm using a navigational controller I need to return correctly. Therefore, this one works best for the final "back" command to the main screen:
[self.navigationController popViewControllerAnimated:YES];
Other options either went back to the Entry Decision (the fault we covered just in case) or did nothing:
[self.navigationController popToRootViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:NULL];
And that is how I'm able to use my existing storyboards, which have been tweaked perfectly for iPhone and iPad over the years of this app's life, and still be able to support Apple's desire to move to scenes. My app needed to use scenes for another purpose, therefore I had no option but to go this route. I went through many attempts to try and trap the scenes as they're getting loaded, but the initial scene seems to get loaded without you, the programmer, getting any options in how it should be or which one to use. You need this pre-view controller to give you that functionality back, and it is one solution which is active in the App Store now.
Be nice.
I created a workspace and added two projects in Xcode workspace. One new and one existing. I want to access a storyboard of an existing project within a tap of a button in new project. Can I do that?
Existing project is linked with a lot of libraries and one of them is Tapku library. It works fine, but I want to have a new project and some buttons there, that when I tap a button I access an existing projects storyboard and then work with that so called application.
thank you.
This is what I tried to do:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
and I have error:
Could not find a storyboard named 'Main_iPad' in bundle NSBundle "somePath/Application1.app (loaded)
This storyboard is in Application2 Project.
My project contains over 60 View Controllers and Xcode is very laggy when loading the storyboard.
How can I resolve this without switching to xibs? I am using Xcode 5.1 and iOS 7.x
Problem
Putting all of your ViewControllers in one Storyboard will significantly slow down Xcode (which renders XML to show your ViewControllers) if the number of the ViewControllers is above 10. Storyboard is not the place to put all of your views, because as its name suggests a board of a specific story.
Solution
Make multiple Storyboards with 5 or 6 ViewControllers each, where you can avoid rendering hell of multiple ViewControllers. You can divide your Application on different boards named by group functionality like LoginStoryboard, UserProfileStoryboard, etc.
You can instantiate a storyboard and present a specific controller by the code below:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Your Storyboard Name" bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:#"Your Controller ID"];
[self presentViewController:viewController animated:true completion:nil];
Keep views in more than one storyboard. It's not the problem if not every controllers are connected with segues. There is no good way to keep a large number of views in a storyboard, in addition to working on a better computer.
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];
}
I'm using the code below to present the initial View Controller from a Bundle.
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Library/ReMoreTesting.bundle"];
NSBundle *bun = [NSBundle bundleWithPath:path];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"ReMoreTesting" bundle:bun];
UIViewController *vc = [sb instantiateInitialViewController];
[self presentViewController:vc animated:YES completion:NULL];
However, presentViewControllerAnimated:completion causes an error to be logged: "Unknown class RMTViewController in Interface Builder file". The layout loads fine, but if I click something which relies on RMTViewController (anything not in the standard ViewController definition), it crashes with "unrecognized selector".
It seems to me that the Storyboard file is loading, but the class file, which is compiled and stored in the same Bundle, isn't being called. Am I missing something which specifies where to look for the class?
Thanks!
Also, yes, this is a dynamically loaded library, and I know the app won't pass Apple's approval for submission to the App Store. Also, I can successfully access the code from another method.
While posting this question, I figured out the problem. I figured I'd post it anyways.
It turns out that loading the Storyboard doesn't load the Bundle's library as well. Calling [bun load] before instantiating the Storyboard resolved the issue.
Everything's easy in hindsight, eh?