Creating a programmatic tab bar with storyboard view controllers? - iphone

I have a tab bar that is created programmatically and I'm having difficulties initializing a storyboard associated with a view.
I'm able to load the view successfully in the tab bar without the storyboard (see code below) but the view is only partially showing because some of the UI components are in the storyboard.
The name of my storyboard is MainStoryboard and I set the storyboard view identifier to SettingsViewController.
How can I initialize my storyboard for SettingsViewController in the code below?
- (void)createTabBarItems {
tabBarController = [[UITabBarController alloc] init];
settingsViewController = [[SettingsViewController alloc] init];
UINavigationController *sett = [[[UINavigationController alloc]
initWithRootViewController: settingsViewController] autorelease];
[sett.tabBarItem setTitle:#"Settings"];
[sett.tabBarItem setImage:[UIImage imageNamed:#"settings.png"]];
[tabBarController setViewControllers:
[NSArray arrayWithObjects:sett, sett, sett, sett, nil]];
}

If you want to initialize the view controller as in the storyboard you have to use the storyboard methods instead of allocating the view controller directly:
// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// either one of the two, depending on if your view controller is the initial one
settingsViewController = [storyboard instantiateInitialViewController];
settingsViewController = [storyboard instantiateViewControllerWithIdentifier:#"SettingsViewController"];

Swift 4
let storyboard = UIStoryboard(name: "Main", bundle: nil)
settingsViewController = storyboard.instantiateInitialViewController()
settingsViewController = storyboard.instantiateViewController(withIdentifier: "SettingsViewController")

Related

iOS: pushing view (SwiftUI) from AppDelegate

We have old project we are trying to convert to SwiftUI but right now depending on the state of couple of things we are loading different StoryBoards. But in our case we just one to replace on storyboard at the time with SwiftUI. Any of you knows how can we accomplish this?
Here is an example of how we are loading each StoryBoard:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *navVc=(UINavigationController *) self.window.rootViewController;
[navVc pushViewController: targetViewController animated:YES];

AppDelegate doesn't init viewController right

I am using PaperFoldMenuController to create a cool SlideMenu in my App.
In the AppDelegate I define the viewControllers and add them to the SlideMenu Array:
// Init viewControllers
RootViewController *rootViewController = [[RootViewController alloc] init];
LivetickerViewController *livetickerViewController = [[LivetickerViewController alloc] init];
// Add view controllers to array
[viewControllers addObject:rootNavController];
[viewControllers addObject:livetickerNavController];
// Add to PaperFoldView
[_menuController setViewControllers:viewControllers];
I use a storyboard for designing most parts of the LivetickerViewController. But all changes are not shown. I checked that Storyboard is connected with the view.
What is the problem in the code ?
If you've designed your LivetickerViewController in the storyboard, then you need to get it from the storyboard not with alloc init.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
LivetickerViewController *livetickerViewController = [storyboard instantiateViewControllerWithIdentifier:#"livetickerViewController"];
Make sure that the name of the storyboard is what is actually in your project, and that you have given the view controller an identifier that matches what you pass in the instantiateViewControllerWithIdentifier: method.
If you made rootViewController in the storyboard, you should do the same thing with it.

How to get an instance of my view controller from the app delegate, storyboards, iOS6

Trying to "instantiate" my initial view controller from the app delegate. I trying to populate an NSMutableArray from the app delegate. A property of the view controller "myMutabelArray" gets an array that is created within the app delegate. With the code below the array is uneffected, even though it's count is 4 (has four objects), as created in the app delegate.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
controller.myMutableArray = mutableArrayCreatedInAppDelegate;
When I log the count from within the AppDelegate I get 4.
When I log the count from within the ViewController I get 0.
I also tried the following which makes me suspect that I am not getting a pointer to the view controller as needed.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
[controller.view setBackgroundColor:[UIColor lightGrayColor]];
Try the following:
ViewController *controller = (ViewController*)self.window.rootViewController;
It will return the initial view controller of the main storyboard.

From One Storyboard to another

For Example, I have 2 Storybards. In the first Storyboard I have a View with a button and when you press the button the second storyboard should appear.
How can I do that?
Take a look at the docs for the storyboard class
You can create a storyboard using
UIStoryBoard *storyboard = [UIStoryboard storyboardWithName:#"secondStoryboard" bundle:nil];
and get view controllers from it like
UIViewController *initialViewController = [storyboard instantiateInitialViewController];
or
UIViewController *otherViewcontroller = [storyboard instantiateViewControllerWithIdentifier:#"otherController"];
Once you've got your view controllers you can just push them onto your navigation controller I guess.
However, I don't know what will happen with using two storyboard objects in the same view hierachy - it's probably fine but you never know :)
Even though your question is a bit confusing, I think I know what you are trying to do.
You want to use two storyboards. UIStoryboard has a method to retrieve an instance of any storyboard with a given name. So first, set a name for your storyboards and view controllers in Xcode and then load them up, and then from within any view controller:
UIStoryboard *anotherStoryboard = [UIStoryBoard storyboardWithName:#"SomeStoryboardName" bundle:nil];
Afterwards, instantiate the desired UIViewController from any storyboard:
UIViewController *anotherViewController = [anotherStoryboard instantiateViewControllerWithIdentifier:#"SomeViewControllerName"];
You could then push it into your navigation stack, for instance:
[self.navigationController pushViewController:anotherViewController animated:YES];
Modifided to be generic here's the way I do this in my app...
UIStoryboard *alternateStoryboard;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPad" bundle:nil];
} else {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPhone" bundle:nil];
}
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
If your app is only iPhone or iPad that can be reduced to...
UIStoryboard *alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate" bundle:nil];
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
You'll probably want to change the last 2 lines to suit the presentation style that you want.

Application tried to push a nil view controller on target <UINavigationController>

I have a UITableView which is assigned to a NavigationController identified by "ArticleNavigation". In my delegate (didFinishLaunchingWithOptions method) I tried to create a UItabBar programatically:
main_tab =[[UITabBarController alloc] init];
viewController1 = [[ListViewController alloc]init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController * nav1=[[storyboard instantiateViewControllerWithIdentifier:#"ArticleNavigation"]initWithRootViewController:viewController1];
tabs_array=[[NSArray alloc]initWithObjects:nav1, nil];
main_tab.viewControllers=tabs_array;
self.window.rootViewController=main_tab;
When I run my app and click on a cell I obtain this error:
Application tried to push a nil view controller on target <UINavigationController>
Any guesses please?