iPhone TabBarController - set selected tab programmatically - iphone

I have two tabs in my app, each of them is a UITableView, and each of the views in the two tabs has its own DetailViewController.
Now, if I click on a TableViewCell in the DetailViewController in the first tab, I want to jump to the DetailViewController of the second tab. I know how to access the second tab
self.tabBarController.selectedIndex = 1;
and I know how to access the DetailViewController, but only without jumping to the second tab.
Is it possible to access the second tab, and then access its DetailViewController?
It would be best if the main TableView in the second tab wouldn't be visible at all, so, it should jump directly to the DetailViewController of the second tab, with the navigation controller showing the "back" button to the main view controller and the second tab highlighted. Is this possible? And, if it is, how can I do this?
Thanks in advance :-)

The tabBarController has an array with the viewController of every tab. You can push the DetailViewController like this:
[[self.tabBarController.viewControllers objectAtIndex:1] pushViewController:detailViewController animated:NO];
Before that you might want to pop to the rootViewController:
[[self.tabBarController.viewControllers objectAtIndex:1] popToRootViewControllerAnimated:NO];

Related

How to take user back to First Tab in TabBarcontroller

I am using a Tabbarcontroller in my application. I have added a tabbarcontroller as a root view to deleagate, also there are 5 tabs in it. Now the problem come I have added a logout button on delegate window it show on top of all view to the right hand side of navigation bar.
I used a logout button so that user can logout from any tabs and go back to the first tab of tabBar controller but it doesn't happen to me. Can any one suggest me how to do.
Try this
[yourTabBarController setSelectedIndex:0]
or
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
You can set the selectedViewController property of your UITabBarController. The documentation says:
"you can use this property to select any of the view controllers in the
viewControllers property"
You could, for example, create a category on UITabBarController with a method such as:
- (void)selectFirstController {
self.selectedViewController = [self.viewControllers objectAtIndex:0];
}

problem with tab bar, not displaying the tab bar upon navigating to that page

In my test project I have some 5 tabs on click of a tab it will go to that corresponding class, on click of back in that screen I will come back to my home page but with out the tab bar.. earlier what 5 tabs were there those are not coming ...
following code I am using under back button
where DataEntry is the class to where i need to navigate
- (void) back_Clicked:(id)sender
{
DataEntry *avController;
UINavigationController *addNavigationController;
if(avController == nil)
avController = [[DataEntry alloc] initWithTabBar];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:avController];
[self. navigationController presentModalViewController:addNavigationController animated:YES];
}
is I have to add that navigation controller to the tab view? how can I get the tab bar on click of back can any one help me ,thanx in advance
As I understand this, you must already be pushing this view controller either via navigation controller or modally. So the idea would be to simply dismiss it right?
If you have used [self.navigationController pushViewController:animated:] then just do [self.navigationController popViewControllerAnimated:YES]; . This should take you back to the earlier view controller.
If you have presented this modally like you have done here, you should do [self.navigationController dismissModalViewControllerAnimated:YES];.
If you need to return back from a view to the previous view using a back button inside a tab view , i would recommend using a navigation controller inside the view for each tab where you require this functionality. Trying to implement a custom back button without using the navigation controller, in my opinion, is only making things tough for yourself.
If you only want one view to be displayed when you tap on a tab bar button then an ordinary view controller inside the tab bar view should suffice.
The tab bar shouldnt be disappearing altogether unless its been implemented incorrectly.

Pushing UIViewController to NavigationController does not actually push the view

I have this code in my table view controller (and delegate):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailStatus *detailViewController = [[DetailStatus alloc] initWithNibName:#"DetailStatus" bundle:nil status:[mStatuses objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
NSLog(#"exiting didselectrow");
}
And in my DetailStatus class:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil status:(NSDictionary *)pStatus {
NSLog(#"I am being called %d", [pStatus objectForKey:#"id"]);
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// some stuff
}
return self;
}
The funny thing is, my DetailStatus is actually being initialised, in the console window, it even outputs "I am being called 000001" but strangely the view is not being pushed to the table view...
I've checked the nib name, and it's ok. I checked the DetailStatus heading file, it looks ok (like this):
#interface DetailStatus : UIViewController {
So does anyone know why the view is not being pushed to the window even if I've initialised it and pushed it?
UPDATE: I tried logging some debugging messages to viewDidLoad in DetailStatus, and it seems like the view is not loaded even though the class was instantiated... I wonder why.
UPDATE2: I have a feeling that this might be my navigation controller organisation that's wrong.. I have this following:
Login page -> customtabbar -> First table view -> DetailStatus
-> Second table view -> DetailStatus
I think I'm only maintaining one navigation controller in that hierarchy. I've never created other navigation controllers. I only push view after another.
Thank you everyone for the answers! I will give out the bounty soon, I'll let other people vote first before the bounty expires.
After looking at it,the scenario seems to be same like me.What I faced for the first time when doing Tab+Navigation.
I am sure that there is some problem with your Tab+Navigation based application.
Although it shows the Tab as well as navigation are not able to navigate the basic flow.And it is very difficult to solve your problem with such less code.
Instead of this, I had an alternate solution for the same:
Once you have a tab bar in a XIB, the easiest way to approach this is to drag a UINavigationController object over from the Library window (looks like a left nav bar button on a gold background) into the Tree View for your tab bar (the text only view, not the GUI). Place it under the tab bar, then drag your existing view controller under the tab bar controller instead of under the tab bar.
When you go to view that tab you should then see a navigation bar on the top of it... if you are loading the navigation controller from another xib, you'll modify the nav bar in the tab bar xib.
else you can below you can follow the best url for the same:
http://books.google.co.in/books?id=2yYlm_2ktFYC&pg=PA179&lpg=PA179&dq=navigation+with+the+tab+based+application+iphoneSDK&source=bl&ots=nf2YYjX5Am&sig=COpHj9wOtsDChQBglpsljSTsElw&hl=en&ei=3ZoFTeGSOI_tsgbc_Iz6CQ&sa=X&oi=book_result&ct=result&resnum=6&ved=0CDAQ6AEwBQ#v=onepage&q&f=false
http://www.youtube.com/watch?v=LBnPfAtswgw
Hope this will surely solve your problem.
Unless the UINavigationController is the immediate parent of your table view controller, [self navigationController] will return nil.
I'm unclear exactly where it lies in the view controller hierarchy, based on your explanation, but I suspect you might have your UITabBarController nested within the navigation controller, when it should be the other way around. If, by chance, you actually mean to push the tab bar off screen (and thus you do want the tab bar controller nested within the navigation controller), you will need to call something like the following:
UIViewController *parentViewController = self.parentViewController;
[parentViewController.navigationController
pushViewController:detailViewController
animated:YES
];
P.S. Once the push is working correctly, you won't need to deselect the tapped row, as UITableViewController does this automatically when the view reappears.
I not seen putting status on the end of the init of the view. Guessing that's something you added.
Dont you just need to do [self pushViewController etc instead of the navigationController bit
Could it be that the nib is not wired up correctly. I.e. In the nib there is no connection between the view and the controller so even though the controller is pushed correctly, the navigation controller cannot find the view.
Just a guess!
You need to implement like above In tab controller put navigation controller according to your tabs and in navigation controller you can put your tab item.And also you need to set tab bar item nib file.then on tapping a tab you get tab screen and because of navigation controller you can easily navigate.even you can navigate on the screen of any tab main screen.
This is the way which you need to implement.
It definitely helps you but you need some logical implementation.
Is your tableViewController directly pushed to the navigation controller or is there another viewController in between ? Check the value of [self navigationController] against the value of your navigationController (from the place your actually instanciate it)
Once check that have you initialized the First Tableview and Second TableView with Navigation controller or not.
While adding to the tabBar, have you added the UINavigationController or only the UIViewController object.
PM_FirstNavigationController *First_navController = [[PM_FirstNavigationController alloc] initWithRootViewController:FirstTableViewController];
tabBarController.viewControllers= [NSArray arrayWithObject: First_navController];
You have to add the view controllers to the tabBar's like that.
Then while calling the [self navigationController], it will give navigation controller object.
Regards,
Satya

iphone view load problem

hii, there
I am making a tabbar application.In this application on the first tab I am using navigation so when I navigate to other view and instantly than I goes to other tab. And again when I am going to first tab it show me navigate view I want to show first tab view.
Somebody can tell me how can I manage these things..
Are you asking how to switch from one tab to another in code? If so it looks like this:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tabcontroller].selectedIndex = [yourIntegerIndexHere];
Where it says "YourAppDelegate," obviously use the name of your actual app delegate.
Where it says "yourIntegerIndexHere," that's an integer with the index of the tab you want to switch to. The left-most tab is 0.
Thats the main purpose of the tabbased application that all your views are saved till they are navigated.
If you want the initial view to be there when you tap on the tabbar then on every tabbars view you have to code for poping your view in the first tab to the rootview controller for that tab.
hAPPY iCODING...
As I understand, you have a NavigationController on your first Tab of your TabBarController and, when you click on the first Tab, you want your NavigationController to go back to its root view controller.
First of all, be aware that this is not the default behavior of a TabBarController and it might be a bit annoying to your users. The user can go back to the rootview of a NavigationController inside a TabBarController by tapping the tab a second time.
Knowing this, if you still want to change the default behaviour of your TabBarController, here is what you can do :
Set your AppDelegate class to be the Delegate of your UITabBarController. It has to implement the UITabBarControllerDelegate protocol, and you have to write something like this :
[myUITabBarController setDelegate:self];
inside your application didFinishLaunchingWithOptions: method.
Then, implement this method inside your AppDelegate.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([tabBarController selectedIndex] == kMyNavigationControllerIndex) {
[(UINavigationController *)[tabBarController selectedViewController] popToRootViewControllerAnimated:NO];
}
}
Where kMyNavigationControllerIndex is a constant value containing the index of the NavigationViewController you want to change (i.e. 0 if it is the first tab).
Hope this helps.

How to switch tabs and pushViewController at the same time?

I have two tabs in my app, the left tab is a normal UIViewController, right tab is a navigation controller with a table view inside it. I'd like to allow the user to press a button on the left tab and jump to a specific detail view that belongs to a specific row in the table view on the right tab.
In the table view itself I have no problems displaying a specific detail view eg:
[self.navigationController pushViewController:theView animated:YES];
and I can also jump tabs using
self.tabBarController.selectedIndex = 1; // 1= right tab
but I cant figure out how to do both. I've tried
self.tabBarController.selectedIndex = 1; // 1= right tab
// alloc and init theView here
[self.navigationController pushViewController:theView animated:YES];
but that doesnt work.
EDIT:
when I try the above code it just switches tab but doesnt push the view.
I also tried what criscokid suggested but nothing happens at all.
EDIT 2:
Trying to explain whats on each tab:
Root (Tab Bar Controller)
|
--(Left Tab)-- UIViewController (I want to go from here...)
|
--(Right Tab)---UINavigationController
|
---UITableViewController
|
----UIViewController (...to here)
^ specific view with data from specific row
in table view (that is 'theView' from above)
For me the correct way was:
UINavigationController * navigationController = (UINavigationController *) [[self tabBarController] selectedViewController];
[navigationController pushViewController:viewController animated:YES];
criscokid's answer has one redundant call to navigationController.
That's because your selectedViewController is actually a UINavigationViewController.
If you are switching the tab bar and pushing the ViewController onto the navigation controller stack in the same method, you wouldn't want to use self.navigationController. If I understand correctly you want to add it to a navigationController on the right tab. I believe you would want to use:
[[[[self tabBarController] selectedViewController] navigationController] pushViewController:theView animated:YES];