UINavigationController inside of UITabBarController issues - objective-c

Can someone point me in the right direction with how to set up a UINavigationController inside of a UITabBarController? I have a feeling I'm using initWithRootViewController wrong.
ViewController1 *viewController1 = [[[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil] autorelease];
self.navController = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
ViewController2 *viewController2 = [[[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil] autorelease];
ViewController3 *viewController3 = [[[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil] autorelease];
ViewController4 *viewController4 = [[[ViewController4 alloc] initWithNibName:#"ViewController4" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
_tabBarController.viewControllers = [NSArray arrayWithObjects:navController, viewController2, viewController3, viewController4, nil];
self.tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Thanks for any tips guys.

In the AppDelegate, you put this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UINavigationController *navC1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UINavigationController *navC2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UIViewController *viewController3 = [[[FirstViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil] autorelease];
UINavigationController *navC3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
UIViewController *viewController4 = [[[SecondViewController alloc] initWithNibName:#"ForthViewController" bundle:nil] autorelease];
UINavigationController *navC4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navC1, navC2, navC4, navC4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Hope that help (Sorry for the bad english :-) );

Related

Add view in tab bar project - xcode

I have a x-code project made by tab bar. On AppDelegate.m I create three buttons for each view:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3=[[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil]autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
When I am on the first view I want to create a button that calls another view:
-(IBAction)btnPush{
[[self navigationController] pushViewController:newView animated:YES];
}
but when the new view comes up it covers the tab bar on the botton. How can I do to fix the problem?
Instead of Adding UIViewController in UITabBarController add UINavigationController as following
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3=[[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil]autorelease];
Now add your ViewController to NavigationController
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
self.tabBarController.viewControllers = #[navController1, navController2,navController3];
It will add Navigation Controller in TabBarController

Programming TabBarController with Navigation in appDelegate

I am trying to include a navigation controller within the third tab of my tabbarcontroller. I have had some feedback but am only able to get about this far. The code below doesn't produce any errors, but does not seem to work as the app just quits out. Does anyone have any input on what I might be doing wrong?
UIViewController *viewController1 = [[FirstViewController alloc]
initWithNibName:#"PDCFirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc]
initWithNibName:#"SecondViewController" bundle:nil];
viewController3 = [[UIViewController alloc] initWithNibName:#"ThirdViewController"
bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc]
initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray
arrayWithObjects:viewController1,viewController2,navigationcontroller, nil];
Thank you all!
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([PDCAppDelegate class]));
}
}
Make sure with all nib names you are using and also with class name, i.e.
if you class name of UIViewController is FirstViewController, nib name should be same. And you have used "PDCFirstViewController" for nib name.
Same with ThirdViewController & SecondViewController.
Try below code...
FirstViewController *viewController1 = [[FirstViewController alloc]
initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *viewController2 = [[SecondViewController alloc]
initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController"
bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc]
initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray
arrayWithObjects:viewController1,viewController2,navigationcontroller, nil];
Here is the solution
UIViewController *courseView = [[[CoursesView alloc] initWithNibName:#"CoursesView" bundle:nil] autorelease];
UIViewController *subjectViewController = [[[SubjectViewController alloc] initWithNibName:#"SubjectViewController" bundle:nil] autorelease];
UIViewController *videoViewController = [[[VideoViewController alloc] initWithNibName:#"VideoViewController" bundle:nil] autorelease];
UIViewController *quizViewController = [[[QuizViewController alloc] initWithNibName:#"QuizViewController" bundle:nil] autorelease];
UIViewController *proifileViewController = [[[Profile2ViewController alloc] initWithNibName:#"Profile2ViewController" bundle:nil] autorelease];
UINavigationController *coursNav = [[UINavigationController alloc] initWithRootViewController:courseView];
UINavigationController *subjectNav = [[UINavigationController alloc] initWithRootViewController:subjectViewController];
UINavigationController *videoNav = [[UINavigationController alloc] initWithRootViewController:videoViewController];
UINavigationController *quizNav = [[UINavigationController alloc] initWithRootViewController:quizViewController];
UINavigationController *profileNav = [[UINavigationController alloc] initWithRootViewController:proifileViewController];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[coursNav, subjectNav,videoNav,quizNav,profileNav];
self.tabBarController.navigationController.navigationBarHidden=YES;
self.tabBarController.delegate=self;
[self.window addSubview:self.tabBarController.view];
[self.window makeKeyAndVisible];
it works for me

Tabbar is not showing OR screen does not reconize touches

i'm using this code for a tabbar, but the tabbar is not showing
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
UIViewController *viewController4 = [[UIViewController alloc] init];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
viewController4, nil];
self.window.rootViewController = tabController;
UIViewController *rootController =
[[xTableViewController alloc]
initWithNibName:#"xTableViewController" bundle:nil];
navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
When i remove this line:
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
The tabbar show's up but i cant 'touch' my screen (i cant touch my screen but the tabbar is 'touchable' because nothing is working, does anyone know how to display the tabbar on a normal way?
It seems like you are initializing the window twice. First with the UITabbarContorller and then with UINavigationController. Try
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
UIViewController *viewController4 = [[UIViewController alloc] init];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
viewController4, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}

How to fill the screen with DetailViewController

I created a project using Master-Detail Application template but i don't need a MasterView. So i deleted Masterview files and codes but this time when i rotate the Simulator/Device in the left side of my main screen a black area stays. I want to stretch my Detail view to fill all the scren but i have no idea how to do. Can anyone please help? Thanks in advance
Note: My app needen NavigationController, firstly i tried SingleView template, but i couldn't push views on that template so i created my app in Master-Detail Application template..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This was the first appearance of the didFinishLaunchingWithOptions:of my ..AppDelegate.m but i changed it with below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//define navigationController in ..AppDelegate.h
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
I answered my own answer in case it may be helpful for someone who lives the same pains:)

SplitViewController with TabbarController

I am using the split View functionality in my app.I have to put tabbar in rootViewController.
But when I add controllers in tabbar and add them into split view it doesnt split.
It only shows detailViewController.
Here is the code in application did finish launching:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIViewController *viewController2,*viewController3,*viewController4;
rootViewController = [[[CategoryItemsList alloc] init] autorelease];
viewController2 = [[[SearchList alloc] init] autorelease];
viewController3 = [[[FavoritesList alloc] init] autorelease];
viewController4 = [[[Information alloc] init] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: rootViewController,viewController2,viewController3,viewController4, nil];
splitDetail = [[splitDetailView alloc] initWithNibName:#"splitDetailView" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:tabBarController];
splitViewController = [[UISplitViewController alloc] init];
// splitViewController.tabBarItem = controller.tabBarItem;
splitViewController.viewControllers = [NSArray arrayWithObjects:nav, splitDetail, nil];
splitViewController.delegate=splitDetail;
rootViewController.splitView=splitDetail;
}
You can refer to this links. I think this is what you are looking out for:
https://github.com/mattgemmell/MGSplitViewController
http://www.raywenderlich.com/1040/ipad-for-iphone-developers-101-uisplitview-tutorial
http://mikebluestein.wordpress.com/2010/04/03/using-a-uisplitviewcontroller-to-create-a-master-detail-ipad-app-with-monotouch/
Once you add the split view then you just need to add the object of the SplitViewController to the TabBarController as one of the viewControllers
Hope this helps.