Removing TabBar Controller when UIBar Button Clicked - ios

In my Application first i have some Login View Controller,after login with button action am moving to Tabbar Controller with Four tabs. my problem is i need to quit that tabbar controller when i click on barbutton and need to come to that loginViewController.please anybody help me in this.actually i have tried some methods but i dint get that.
Thanks in Advance for Valuable Solution to problem.

In appDelegate place your tabBar and in side loginView ,add this tabBar
TUTAppDelegate *appdelegte =(TUTAppDelegate*)[[UIApplication sharedApplication]delegate];
[[appdelegte window]addSubview:[[appdelegte motivationalTabBar]view]];
On button press remove TabBar
TUTAppDelegate *appDelegate = (TUTAppDelegate *)[[UIApplication sharedApplication] delegate];
[[[appDelegate motivationalTabBar] view]removeFromSuperview]

Add your tabBar controller to [appdelegte window] first and then present Login View Controller on it. When user log-ins successfully, dismiss Login View Controller. Again when you click on barbutton to come to that loginViewController , present Login View Controller on tabBar controller. In this case there is no need to add and remove tabBar again and again.

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate.tabBarController.view removeFromSuperview];
[delegate.window addSubview:delegate.loginViewController.view];
i got solution for my problem with this code.
hope this will help for someone.
thanks for all answers.

Related

how to hide status bar in ios from a current VC when presenting another VC modally?

How would I go about hiding a status bar from the current VC right before I present another VC modally?
I don't want to change it through the entire app, just when I'm about to present another vc then when I come back to the first VC I want the status bar to come back.
what I've tried:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationFade];
Don't call setStatusBarHidden. Implement prefersStatusBarHidden appropriately in each view controller.
-(BOOL)prefersStatusBarHidden{
return YES;
}

How to find the current view controller in iOS

The entrance of my app is a UINavgationViewController. The root view controller is a UITabBarViewController. The UITabBarViewController contains some normal UIViewControllers. These controllers may call:
self.navgationController.pushViewcontroller(otherViewController, animated:true);
However, I have another thread running in the background. How can I know which view controller is currently being shown on the main screen?
I tried to call UIApplication.keywindows.rootviewcontroller but it returns UINavgationViewController.
UINavgationViewController.viewcontrollers.count == 1, it only contains a UITabBarViewController.
I think you can get the selected view controller from your tab bar:
navigationController.tabBarController.selectedViewController
try by using viewDidAppear method in every view controller class
- (void)viewDidAppear:(BOOL)animated
{
NSLog(#"Currennt view:%#",self);
}
self returns name of ViewController class
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(#"%#",appDelegate.navController.topViewController);
You can get the top most Controller that is pushed.

access tab bar view controller in app globals

How do I access the tab bar view controller from app globals? The tab bar view controller is set up in storyboard. I dont want to create a new one but get the existing one.
I guess the UITabBarViewController is set as the app's window rootviewController. If that's the case , you can get it like this:
AppDelegate *myAppDelegate = (AppDelegate *)[ [UIApplication sharedApplication] delegate];
UITabBarViewController *myTabBarViewController = (UITabBarViewController *) myAppDelegate.window.rootViewController;
, where AppDelegate is the name of your App Delegate class.
Hope this helps.
Regards,
George

Logout from tab bar controller to Root view controller

when the app is launched, i have placed a view controller(login) after validating the field it is redirected to tab bar controller. The problem is i have to place logout button and when clicking logout button it should go to the root view controller(login page). I have tried pushing from tab bar controller to root view controller, it is pushed but still facing few tab bar issues while proceeding further. How can i pop/push to root view controller from tab bar item ?
I'd imagine in your AppDelegate.m, You have created a navigation controller with the LoginUIViewController as the RootViewController.
You could solve the problem like this:
For example, you have a FirstTabUIViewController in your TabBarController, you want to go back to your LoginUIViewController (your RootViewController) from the FirstTabUIViewController.
Create a reference to your TabBarController in the FirstTabUIViewController.h and .m
#property (strong, nonatomic) IBOutlet UITabBarController *tabBarController;
#synthesize tabBarController = _tabBarController;
Create a method handles "LogOut" button click in .m
-(IBAction)logoutBtnTapped:(UIBarButtonItem *)sender{
[self.tabBarController.navigationController popToRootViewControllerAnimated:YES];
}
That it is! Hope that helps :)
May be you can use UINavigationController for root view controller http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
There are some examples about custom back buttons. If you want to use default back button you can rename as logout and give an action on it.
Just an idea.
You just need to place the login screen in appdelegate window again when you clicked the logout button.
LoginViewController *loginVC = [[LoginViewController alloc]init];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window setRootViewController:loginVC];// This will initiate the login screen again
This works fine for me in same case,
ChooseStateViewController *loginVC = [[ChooseStateViewController alloc]initWithNibName:#"ChooseStateViewController" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:loginVC];
[nc.navigationBar setTintColor:[UIColor blackColor]];
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.window setRootViewController:nc];

Switching tabs programmatically not working properly

I'm trying to switch tabs programmatically using:
[tabBarController setSelectedIndex:index]
I've also tried:
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:index];
It works the first time - switching both tabs and the view associated with the tab. However it doesn't work the second time and thereafter. Then it erratically switches the tab (not always), and doesn't switch the view controller associated with the tab.
Any ideas?
Here's what I'm trying to accomplish:
Tab A: I have a tab that launches the camera to take a picture and add some details.
Tab B: I have a tab with a list of pictures taken and a bar button to add a new item by taking a picture and adding details.
I'm trying to make it so that when the user taps Tab A it switches to Tab B and launches the add item method. What's the best way to do this?
Here is more detailed code:
Tab A is hooked up to navigation controller with a UIViewController. In that controller I have the following:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate switchView];
}
In AppDelegate.m I have the following:
- (void)switchView
{
tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedViewController
= [tabBarController.viewControllers objectAtIndex:3];
}
The UIViewController class has a tabbarController property, so you can simplify things by calling the view controller's parent controller (the tab bar controller) instead of using the app delegate to access the tab bar controller. Calling setSelectedIndex should be fine too. So from inside your view controller:
[self.tabbarController setSelectedIndex:3];
To help with debugging, you could put some NSLogs in relevant places:
NSLog(#"The currently selected tab is: %#",self.tabbarController.selectedIndex);
It turns out the problem was that I was changing the tabBarController index from viewWillAppear instead of viewDidAppear. Must be something to do with the order in which things are loaded.

Resources