setting a tab bar controller view controller programmatically - iphone

Is there a way to set the view controller on the tab bar controller programmatically? Lets say I want it to show the second's tab view controller programmatically, is there a way to do that?
This is useful if I logout from my app, which is done from my third tab, when the user logins it should start from the 1st tab again. When I logout I am just showing a present modal view controller on top of what the previous view is, so I somehow needs to reset it again to the first tab bar without re-initializing it all over again.
The issue is now how do I do this?

From Apple's documentation it looks to me like you could just call the following two functions:
[myTabBarController setSelectedIndex:0];
[myTabBarController setSelectedViewController:[myTabBarController.viewControllers objectAtIndex:0]];

Hi ye you can do this
You might have tabbarcontroller object in appDelegate.
So on logout button
make object on your appDelegateClass and do this:-
appDelegate.tabBarController.selectedIndex=0;

Have a look at the reference on UITabBarController. Work with selectedIndex and selectedViewController.

Related

Tab Bar mysteriously disappears?

Attached are two images. The first shows my current main.storyboard, and the second shows my problem when I run the app. I have a tab bar controller that has two tabs. On the first tab there is a button. When pressed, the button goes to another view controller with content. At the top is a Navigation bar with a back button. After viewing content, I press the back button, and am back on the original page with the button, but the tab bar is missing. I have seen a few answered questions, but it appears they made their tab bar in the view controller instead of the storyboard. Another says that I should never return to a previous view unless I use an unwind segue. Is this true? If so, how do I set up an unwind segue. If not, how do I fix this problem otherwise? Thank you.
http://i.stack.imgur.com/IYmX2.png
http://i.stack.imgur.com/7slt5.png
The problem is in the wiring of your ViewControllers. You have probably embedded your UITabBarController inside the UINavigationController and not the other way around.
A correct layout looks like this in Interface Builder :
To reproduce:
In Interface Builder drop a UITabBarController. This will come with 2 UIViewController's already wired in.
Pick one of the UIViewController's (let's call it VController1) and click on Editor / Embed in / Navigation Controller. This wires the VController1 to live inside a UINavigationController that is inside the UITabBarController
Add a 3rd UIViewController next to VController1 Let's call it VController3
Wire in a segue between VController1 and VController3, for example with a button.
I hope that's clear enough
Try Linking the button in your viewcontroller (other than the views of the tabbed bar controller) with the tabbed bar controller. Create a segue that links the button with the controller of the tabbed bar application

Resetting Tab bar controller's view controllers

In my Application I have creted view controllers for Tab Bar using XIB. I mean I added view controllers in tab bar using XIB. See picture,
So my application is like this. I have a log out functionality that will show login page. From Login page I have displayyed tab bar as
[self.view addSubview tabBarController.view];
When I log out I simply remove this tab bar and show login screen as
[self.tabBarController.view removeFromSuperView];
Then It will show login screen. When I login again, The view controllers inside tab bar is not reset.
I mean If I have showed some data like names in table view, It will be there as it is when I logged in again (some time I login as different user and I get details for previous user.). I tried
self.tabBarController.viewControllers = nil;
to reset all view controllers, but it not works any more. I think each time When I log in all view controllers should call ViewDidLoad:, currently it is calling viewWillAppear:. So please let me know how could I resolve this issue
I think since you are not using a fresh object once you sign out and sign in again, it is showing you with the last stage. Try initializing your controller object after you have logged in may work for you.
Let me know if you find any problem further.
Cheers
Tarun
All it's troubles because you use Interface Builder. Ok, try to explain. In your class like MyViewController.m ypu need set property like this:
#interface MyViewController : UIViewController
{
IBOutlet UITabBarController *mytabbar;
}
In IB (Interface Builder) you need set mytabbar property name for your UITabBarController. After that you can rectreate this variable, because you have pointer to this variable.

iPhone TabBar Controller Usage

I want make a typical tabbar based application.
And I want to insert login view on startup before main tabbar is shown.
I tried to insert TabbarController in main view, but cannot find appropriate code. All sample code I found is insert TabbarController on startup. (in Delegate file)
My fellow suggest create toolbar on startup but hide it at login view, but I am not sure if this is a general method or not.
In this case, what is a 'recommanded' handling method of TabbarController ?
A simple solution would be to
1-Add the tab bar to UIWindow in the appDelegate as is suggested by apple.
2-then add a UINavigationController in tabbar using tabbarController.viewControllers=[NSArray arrayWithObject:yourNavController];
3- Now after alloc init on your login controller write this code before pushing it to the navigation controller of the tabbar
yourLoginController.hidesBottomBarWhenPushed=true;
4- push yourloginViewController to the navigation controller of the tabbar.
5- After authentication before pushing your MainviewController instance on the navigation set it like this
MainviewController .hidesBottomBarWhenPushed=false;
I hope these five simple steps will do the magic for you cheers :)
Please let me know if it helps you.Thanks
The approach I use for login screens which works great is:
prepare and show the regular main screen (tab bar controller with whatever initial VC you want to use)
immediately present the login screen modally (without animation) from the tab bar controller (which will obscure the tab bar controller, which is what you want)
make the login screen the startup image

Iphone Log Out from App problem

i have an application based on an navigation controller. At first, it presents a Modal View with the Log In interface. After the user logs in, i dismiss this view and present the rootViewController.
Then, in the nav bar i have a button in order to show the Profile view in wich i can log out.
The problem is the following. When i press log out in the Profile View i do two things:
Present a new Modal View with the log in screen.
Pop the profile view controller.
But for a moment, i can see the pop action and the RootViewController before the Log In View is shown.
How can avoid this behavior?
Thanks
You need to specify that you do not want to see the animation. Where are you popping the last view controller from?
It should probably be something like this
[navController popViewControllerAnimated:NO];
Hey,
I think you must have the animated flag either on - (void)dismissModalViewControllerAnimated:(BOOL)animated or
on - (UIViewController *)popViewControllerAnimated:(BOOL)animated set to YES so you are actually able to see it switching views. If you change them to NO the "Log in" view will come up right away.
I hope this is useful for you.
Regards

iPhone Development, How to Programmatically Call a NavigationController back button from another tab?

Here's the setup:
I have a tab bar controller with two tabs. There is a navigation controller on top the second tab, so that I can view details from a table.
On tab #1, I have a button. When this button is pressed, it switches the selected tab over to tab #2.
My problem is this: Let's say I go to tab #2, then I select a line from my table to view the detailed information. Then, instead of hitting the "back" button to return to the base view, I select tab#1. Then I press my button. Tab#2 loads, but it is still showing the detailed view, instead of the table view that I want.
I know how to programmatically press the button.
[self.navigationController popViewControllerAnimated:YES];
But how would I do that when the button pressed method is inside the class for tab#1, which knows nothing of tab#2's navigation controller?
Hope that makes sense. Thanks in advance for any help!
Okay, I finally got it to work. The solution that was suggested gave me a good jumping off point. Here is the code that finally worked for me:
ARFinderAppDelegate appDelegate = (ARFinderAppDelegate)[[UIApplication sharedApplication] delegate];
[appDelegate.booksNavigationController popViewControllerAnimated:YES];
self.tabBarController.selectedIndex = 1;
I had to access the class that contained the NavigationController through an instance of my application delegate. I also had to remove the "objectAtIndex:1" because my application would crash.
Thanks for all the help, this has been a great learning experience for this iPhone development newbie!
use your delegate singleton.
[[delegate.tabBarController.viewControllers objectAtIndex:tab2Index] popViewControllerAnimated:YES];