instantiating a Storyboard as a TabBarItem - iphone

I have an old project that has it's view implemented as xibs. The views are controller with a tabbar.
Now I want to add a new UITabBarItem, but I want that this new item is not xib-based, but Storyboard based. Meaning, I want to use a storyboard just for the content of this tabbar item, and leave the rest of the app as it is.
I did the following:
created the new storyboard
created a new ViewController that should be the wrapper for the storyboard
then I changed the underlying VC in IB of the Tab to the new VC
But how to I proceed now? I tried this to make the InitialViewController the ViewCOntroller of the the Tab:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
UIStoryboard *financeStoryboard = [UIStoryboard storyboardWithName:#"WS_Finanzierung_Storyboard_iPhone" bundle:nil];
UIViewController * initialViewController = [financeStoryboard instantiateInitialViewController];
[self presentViewController:initialViewController animated:YES completion:nil];
}
return self;
}
But it doesn't work (crashes yet..).
Q: is that the right way to do it anyway? Is there a best practice doing this?
Thanks in advance

Try this one..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[StoryViewController alloc] init];
UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
viewController3 = [tableViewStoryboard instantiateViewControllerWithIdentifier:#"newTab"];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Assign storyboard identifier like this -->
and uncheck is initial view controller -->
Output ll be like this..

Related

How to link nested viewcontrollers using the child viewcontroller pattern

Compiling and running using iOS 7 - getting warning message: "Presenting view controllers on detached view controllers is discouraged" while presenting modal view controller. I have learned that viewcontrollers with linked using the child viewcontroller pattern will not produce warning. Can someone suggest way to link nested viewcontrollers using the child viewcontroller pattern to avoid warning message.
(void)applicationDidFinishLaunching:(UIApplication *)application
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
self.loginRootviewController = [[MainViewController alloc] initWithNibName:#"MainViewController-iPad" bundle:nil];
}
else
{
self.loginRootviewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.loginRootviewController];
DDMenuController *rootController = [[DDMenuController alloc] initWithRootViewController:navController];
_menuController = rootController;
AppMainMenuViewController *leftController = [[AppMainMenuViewController alloc] init];
rootController.leftViewController = leftController;
self.loginRootviewController.delegateLogin = leftController;
self.window.rootViewController = rootController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
}
instead of using DDMenuViewController use SWRevealViewController. It is updated for iOS 7 and has more functionality than DD

tabBarItem image and text not showing on UITableViewController

I just created a new iPhone tabbed Application using xcode project template, without storyboard. I deleted the FirstViewController and SecondViewController which was generated by the project template.
Later, I created new controllers called MyFirstViewController (subclass of UITableViewController) and MySecondViewController (subclass of UIViewController) and put them under UINavigationControllers
I changed the code a bit so now it looks like below:
appdelegate.m :
#import "MyFirstViewController.h"
#import "MySecondViewController.h"
....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//create view controllers
UITableViewController * vc1 = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
UIViewController * vc2 = [[MySecondViewController alloc] initWithNibName:nil bundle:nil];
//create navigation controllers
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
//add nav controllers to tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[nav1, nav2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
tabBarItem is NOT SHOWING on first tab
MyFirstViewController.m :
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Setting tabbar item and image here is not showing. It just show empty tab, no title and no image. why o why?
self.tabBarItem.image = [UIImage imageNamed:#"someImage"];
self.tabBarItem.title = #"someName";
}
return self;
}
but the second tab is working just well
MySecondViewController.m :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// This is showing just fine
self.tabBarItem.image = [UIImage imageNamed:#"someImage"];
self.tabBarItem.title = #"someName";
}
return self;
}
Any idea why a subclass of UITableViewController can't show the tabBarItem? meanwhile UIViewController can show it just fine.
In your didFinishLaunchin method you have
UITableViewController * vc1 = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
But in your MyFirstViewController you
- (id)initWithStyle:(UITableViewStyle)style;
You need to call this method instead on initWithNibName, as this method doesn't exist your tabBarItem is not showing up.

Tabbar controller load empty xib

I'm trying to build an app that have main view at the beginning with 3 buttons, then when the user press any button of these buttons, the tab bar view will appear with selected tab bar item.
My problem is here, when the tab bar view should appear ... it appears empty !!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
MainMenuViewController *mainMenuViewController = [[[MainMenuViewController alloc] initWithNibName:#"MainMenuViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:mainMenuViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
//Button Action in main menu view
-
(IBAction)button1Action:(id)sender {
TabbarViewController *tempView = [[TabbarViewController alloc] initWithNibName:#"TabbarViewController" bundle:nil];
[self.navigationController pushViewController:tempView animated:YES];
[tempView release];
}
You have to set viewControllers property of your TabbarViewController(of course if it is superclass of UITabBarController). Create your 3 viewControllers in init method of TabbarViewController, add them to array and set it as viewControllers property like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(#"%#",[[self class] superclass]);
UIViewController *yourFirstViewController = [[UIViewController alloc] init];
UIViewController *yourSecondViewController = [[UIViewController alloc] init];
UIViewController *yourThirdViewController = [[UIViewController alloc] init];
yourFirstViewController.title = #"First";
yourSecondViewController.title = #"Second";
yourThirdViewController.title = #"Third";
NSArray *threeViewControllers = [[NSArray alloc]initWithObjects:yourFirstViewController, yourSecondViewController, yourThirdViewController, nil];
self.viewControllers = threeViewControllers;
// Custom initialization
}
return self;
}

Combine UITabBarController with UINavigationController

I tried to use an "tabbed application" with a navigation bar in it. With the default the tab bar works fine, but I just can't gat a navigation bar. I found some stuff about pushing the navigation-bar and stuff like that, but all the stuff I found was some years ago, so don't gonna help me. And the recent stuff is outdated to, since iOS5 and the new version of Xcode..
Could anyone point me in the right direction to combine a to solve this problem?
Keep the following facts in mind please:
I'm developing for iOS5
I'm using Xcode 4.2
Here's how you can achieve it programmatically.
Delete the reference to your main xib in [appName]-Info.plist
In main.m, load your delegate:
int retVal = UIApplicationMain(argc, argv, nil, #"myAppDelegate");
In the app delegate, load the tabBar, the navigation controller and the view in the navigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create window since nib is not.
CGRect windowBounds = [[UIScreen mainScreen] applicationFrame];
windowBounds.origin.y = 0.0;
[self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]];
// View Controllers for tabController (one viewController per tab)
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// first tab has view controller in navigation controller
FirstView *firstView = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstView];
[viewControllers addObject:navController];
SecondView *secondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[viewControllers addObject:secondView];
// create the tab controller and add the view controllers
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
// add tabbar and show
[[self window] addSubview:[tabController view]];
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSMutableArray *arrayViewController = [[NSMutableArray alloc] init];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[arrayViewController addObject:navigationController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[arrayViewController addObject:navigationController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = arrayViewController;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
In iOS 5 it is no longer tolerated to change the view controller for a tab (no problem before iOS5). The only accepted controller is that defined in IB for that tab. So it is neccessary to install a navigation controller on this tab and give his view the navigation bar. Then you can push or pop your desired views without changing the tab's controller.
The basic theory is that you create a UITabBarController, and then put a UINavigationController inside that, and then put a UIViewController as the root view controller of the navigation controller. bryanmac just answered with a good code sample.

ModalView doesnt show up with tabbarcontroller

I am building an app that has a login screen that leads to a tabbar. I followed this example on how to push a modal view once you launch the app (as the "sign in" page) and then dismiss it.
Example --> Show / Hide tab bar
From some reason, it's not really working - when I launch the app, I see the tabbar view with the two view controllers. no sign in page.
here is my code:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, secondNavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
My first tab (which is where I understand all this modal view business needs to happen)
.h
#interface FirstTab : UIViewController
#end
.m
//UPDATED CODE PER COMMENT BELOW
- (void)viewDidLoad
{
[super viewDidLoad];
SignIn *loginview = [[SignIn alloc] initWithNibName:#"SignIn" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: loginview];
self.hidesBottomBarWhenPushed = YES;
[self presentModalViewController:controller animated:YES];
}
And of course, I dismiss the modal view in the SignIn view controller, though I never get there as I mentioned.
What am I doing wrong here? Thanks for the help!!
You could use :
[[self tabBarController] presentModalViewController:controller animated:YES];
since first viewController1 is your first tab, and self.navigationController might be nil.
In your custom view controller subclass called SignIn implement initWithNibName:bundle: instead if init.
-(id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// init here
}
}
Now when init/alloc it call either :
SignIn *loginview = [[SignIn alloc] initWithNibName:#"SignIn" bundle:nil];
if your interface is in a NIB file, or :
SignIn *loginview = [[SignIn alloc] initWithNibName:nil bundle:nil];
if there no NIB.
Also why putting it as a root view controller of any navigation controller ? unless you need to go deeper in some model data presentation, just present it directly :
// why ?
//UINavigationController *controller = [[UINavigationController alloc]
// initWithRootViewController: loginview];
//self.hidesBottomBarWhenPushed = YES;
//[self presentModalViewController:controller animated:YES];
[self presentModalViewController:loginView animated:YES];
You have to include this in your code,
yourModelController =[[Yourmodelcontroller alloc]init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: yourModelController];
self.hidesBottomBarWhenPushed = YES;
[[self navigationController] presentModalViewController:controller animated:YES];
[controller release];
hope this will help you out.
I've encountered issues where modals don't like showing from viewDidLoad. Try adding your code to your viewWillAppear and it should show.