How to get a new view controller instead of parent view for a 1st tab in tabbar view controller? - ios

I struck in a problem with using tab bar controller to my view controller.
Here is my problem
I have a view controller for that i embedded a tab bar controller and i have added four tabs for it and three views are created for the other three tabs, here is my Exact problem. i don't want my 1st tab bar controller select default if i click on my 1st tab i want to show the other view it should not display my parent view where i am showing all the tabs in a parent view.
Please give me some solution how to overcome this. hope my question is clear with details.

Somehow I am able understand your problem. Without posting code sometime its very difficult to identify exact problem.
I am sharing a code with you , assume that App launch a single view controller as "Login View Controller" which is follow by "Dashboard View Controller". Now you want to show TabBar after user login with four different tabs.
Please called below method "setUpTabBar" within Dashboard View Controller ViewDidLoad.
- (void)setUpTabBar
{
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"Tab1";
vc1.view.backgroundColor = [UIColor clearColor];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:vc1];
nav1 =[self setTrasparentNav:nav1];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"Tab2";
vc2.view.backgroundColor = [UIColor clearColor];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:vc2];
nav2 =[self setTrasparentNav:nav2];
UIViewController *vc3 = [[UIViewController alloc] init];
vc3.title = #"Tab3";
vc3.view.backgroundColor = [UIColor clearColor];
UINavigationController *nav3 =[[UINavigationController alloc] initWithRootViewController:vc3];
nav3 =[self setTrasparentNav:nav3];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[nav1,nav2,nav3];
tabBar.selectedIndex = 0;
tabBar.view.frame = self.view.bounds;
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];
}
You can change default selected tab bar using tabBar.selectedIndex = 0;
Hope this will help you.
Update (As requested by questionnaire):-
UIView *viewBottom=[[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height- 60.0, self.view.bounds.size.width, 60.0)];
viewBottom.backgroundColor =[UIColor grayColor];
UIButton *btnAdd =[UIButton buttonWithType:UIButtonTypeSystem];
btnAdd.frame=CGRectMake(10, 10, 60.0, 40.0);
[viewBottom addSubview:btnAdd];
[self.view addSubview:viewBottom];

If you are trying from code, first see how to make uitabbarcontroller in code: How to programmatically add a UITabBarController & UINavigationController in AppDelegate?

Related

Adding a persistent footer underneath a UITabBarController

I've inherited a project for which I want to add a persistent footer area beneath the entire app. The app is using a UITabBarController that is always shown other than for a login screen. The tabBarController is created as follows:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
UIStoryboard *sb1 = [UIStoryboard storyboardWithName:#"SB1" bundle:nil];
// ... same for sb2 and sb3
[tabBarController setViewControllers:#[sb1, sb2, sb3]];
[tabBarController setSelectedIndex:0];
I've tried manually setting tabBarController.size.height, but it doesn't seem to have any effect. I've never used storyboards before, is there some way to do initWithFrame when using them? Or am I approaching this completely the wrong way?
Thanks werediver, you got me on the right track. I got it working by adding the UITabBarController as a child view controller and then manually adding its view as well.
const CGFloat FOOTER_HEIGHT = 20;
// Set up tab bar area
CGRect contentFrame = [UIApplication sharedApplication].delegate.window.frame;
contentFrame.size.height -= FOOTER_HEIGHT;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = contentFrame;
// Set up footer
CGRect footerFrame = contentFrame;
footerFrame.origin.y = contentFrame.size.height;
footerFrame.size.height = FOOTER_HEIGHT;
UIView *footer = [[UIView alloc] initWithFrame:footerFrame];
// Create outer view controller
UIViewController *outer = [[UIViewController alloc] init];
[outer addChildViewController:tabBarController];
[outer.view addSubview:tabBarController.view];
[outer.view footer];
[[UIApplication sharedApplication] keyWindow].rootViewController = outer;

UINavigationController issue - if i return to first view, i see the color of second view

I am creating UINavigationController and it's UIViewControllers like this. First with yellow color, second with blue color.
Then adding it to stack. Yellow is displayed for a while, then the second blue.
What's the problem - if i press the button in top bar to get back to previous Controller, i don't see yellow background but blue, thought the title of window is "One" which is correct.
Why is this happening? Thx
UIViewController *one = [[UIViewController alloc] init];
[one.view setBackgroundColor:[UIColor yellowColor]];
[one setTitle:#"One"];
UIViewController *two = [[UIViewController alloc] init];
[two.view setBackgroundColor:[UIColor blueColor]];
[two setTitle:#"Two"];
UINavigationController * navController = [[UINavigationController alloc] init];
[self.view addSubview:navController.view];
[navController pushViewController:one animated:YES];
[navController pushViewController:two animated:YES];
I figured out what's the reason.
Because navController was relased from the memory.

RESideMenu: Add new viewcontrollers before RESideMenu

I am using RESideMenu in my application. But I need to add login and registration viewcontrollers before the RESideMenu.
Is it possible, if yes then how can I do that ?
Thanks in advance.
There are many ways to do this. The most common is you have a loginView controller and then in the app delegate you can write something like this in the app delegate:
if([[NSUserDefaults standardUserDefaults] valueForKey:#"AlreadyLogin"])
{
// So, here user already login then set your root view controller, let's say `SecondViewController``
SecondViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:#"SecondViewController"];
// then set your root view controller
self.window.rootViewController = secondViewController;
}
else
{
// It means you need to your root view controller is your login view controller, so let's create it
LoginViewController *loginViewController= [storyBoard instantiateViewControllerWithIdentifier:#"LoginViewController"];
self.window.rootViewController = loginViewController;
}
Credit: Skip view if user already logged
Yes it is very possible.
Solution A:
After successful login/sign up, do:
[UIApplication sharedApplication].window.rootViewController = [[RESideMenu alloc] init...];
Solution B:
Place your login/signup view controllers in the main content portion of the RESideMenu, and disable the two side menus until the user is signed in.
Solution C:
Embed the RESideMenu in a UINavigationController and optionally hide the navigation bar.
For more info I recommend researching "view controller containment" as that is the pattern used by RESideMenu, UINavigationController, and other types of "container" view controllers.
I hacked together a quick example of Solution C and it seems to work fine:
#implementation LoginViewController
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 100, 100);
[button setTitle:#"Login" forState:UIControlStateNormal];
[button addTarget:self action:#selector(goToRESideMenu) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.navigationController.navigationBarHidden = YES;
}
- (void)goToRESideMenu {
UIViewController *redViewController = [[UIViewController alloc] init];
redViewController.view.backgroundColor = [UIColor redColor];
UIViewController *greenViewController = [[UIViewController alloc] init];
greenViewController.view.backgroundColor = [UIColor greenColor];
UIViewController *blueViewController = [[UIViewController alloc] init];
blueViewController.view.backgroundColor = [UIColor blueColor];
RESideMenu *sideMenu = [[RESideMenu alloc] initWithContentViewController:redViewController
leftMenuViewController:greenViewController
rightMenuViewController:blueViewController];
[self.navigationController pushViewController:sideMenu animated:YES];
}
#end
The result looks like this:

how to add tab bar controller from the second view controller [duplicate]

This question already has an answer here:
Showing login view controller before main tab bar controller
(1 answer)
Closed 9 years ago.
Im beginner to IOS app development learning.
I have a login screen as my first view controller and i need the second view controller to be a tab bar view controller .with 4 different tabs and i have 4 different XIB's for them.
some one help me to go ahead.
Best way you can do is Present the login screen modally when the app starts from your tab bar controller first screen, add code for presenting login screen in viewWillAppear and after login dismiss the screen. You can create TabBarController in appDelegate like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController tabBarController=[[UITabBarController alloc] init];
FirstViewController *firstVC = [[UIViewController alloc] initWithNibName:#"FirstVC" bundle:nil];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[UIViewController alloc] initWithNibName:#"secondVC" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
tabBarController.viewControllers = [NSArray arrayWithObjects: firstNavController, secondNavController, nil];
tabBarController.selectedIndex=0;
tabBarController.delegate = self;
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:#"Movies" image:[UIImage imageNamed:#"MoviesTAB.png"] tag:1];
[firstVC setTabBarItem:item1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:#"Music" image:[UIImage imageNamed:#"musicTAB.png"] tag:2];
[seconfVC setTabBarItem:item2];
tabController.tabBar.translucent = NO;
tabController.tabBar.barStyle = UIBarStyleBlack;
tabBarController.tintColor = [UIColor whiteColor];
self.window.rootViewController = tabController;
return YES;
}
Best way is use storyboard and there just have one initial UIViewController and from that make segue to UITabBarViewController.
http://youtu.be/a_DCTSTv1Mw
If you want to make it through xib make a UITabBarViewController and add viewControllers to the array of object of that UITabBarViewController's object.
Sample code :
NSMutableArray *arrViewControllers = [[NSMutableArray alloc] init];
UIViewController *tabController;
UIImage *tabImage ;
NSString *tabTitle;
for (int i= 0; i < 3; i++) {
switch (i) {
case 0:
tabController = [[ViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon1.png"];
tabTitle = #"Text";
break;
case 1:
tabController = [[ImageDemoViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon2.png"];
tabTitle = #"Image";
break;
case 2:
tabController = [[TableDemoViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon3.png"];
tabTitle = #"Table";
break;
}
// set the image and title using some properties of UIViewController
tabController.tabBarItem.image = tabImage;
tabController.tabBarItem.title = tabTitle;
//add objects to array
[arrViewControllers addObject:tabController];
[tabController release];
}
_baseController = [[UITabBarController alloc] init];
_baseController.viewControllers = arrViewControllers;
[arrViewControllers release];
go to your appDelegate
1.create a viewController for login screen.
LoginViewController *viewController1 = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
2.create a navigationController with root view your login ViewController.
UINavigationController *nVC = [[UINavigationController alloc] initWithRootViewController:viewController1];
3.make navigationController to root view of window.
self.window.rootViewController = self.nVC;
[self.window makeKeyAndVisible];
Now go to Touch-Up-Inside method of login button in LoginViewController.
1.After validation of password and userId initialise your viewControllers for tabbar and TabbarViewController.
UiViewController ...*yourViewControllers,..,
UITabBarController *YourtabBarController = [[UITabBarController alloc] init];
2.Now add these viewControllers to your tabbarController.
YourtabBarController.viewControllers = #[ YourViewController1,YourViewController2,YourViewController3,......];
3.Finally push this tabbarController to navigationControllere.
[self.navigationController pushViewController:YourtabBarController animated:NO];

TabBarController within the RootViewController of a SplitViewController

I'd like to develop an iPad app which would be composed of a SplitViewController. I'd like to add a TabBarController in the RootViewController.
I'm at the very beginning of the development. So I've started to simply create a new project, add a SplitViewController via Interface Builder and test the app, no problem of course. Then I've tried to add the TabBarController to the RootView, no problem either via the Interface Builder. The problem I have there is that I can't make the app rotate with the device. I assume that I have to change something in the code but I don't know what :-(
I've noticed that the method shouldAutorotateToInterfaceOrientation is never called when the device rotates.
I'm sorry to ask this question but I'm very new in developping iPad/iPhone apps.
Best regards
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:annualViewController];
[navigationController1.navigationBar addSubview:imageView1];
[list addObject:navigationController1];
[imageView1 release];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:rootViewController];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navigation bar.png"]];
imageView2.frame = CGRectMake(0, 0, 320, 44);
[navigationController2.navigationBar addSubview:imageView2];
[list addObject:navigationController2];
[imageView2 release];
tabBarController.viewControllers=list;
detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
rootViewController.detailViewController = detailViewController;
annualViewController.detailViewController=detailViewController;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:tabBarController, detailViewController, nil];
splitViewController.delegate = detailViewController;
// Add the split view controller's view to the window and display.
[window addSubview:splitViewController.view];
[window makeKeyAndVisible];

Resources