how to use UITabBarController's shouldSelectViewController delegate method - iphone

I have a tabbar-based application (tabbar controller is added in window itself) and all the navigation controller with their respective root view controllers are being set in window's xib. I have 4 tab bar items.
Suppose I click on item 1, then the root view controller for that item is being shown to me. This root view contains a table with 5 cells. If I click on a row, then a new view is pushed onto the navigation stack. Now, this pushed view has a button clicking on which will again push a new view controller. I have 4 such view controllers which are getting pushed one after the other on navigation stack.
Now, lets say I am on 3rd view in navigation stack and then, I have clicked on tab bar item 1 (the same on which I clicked earlier); then, the first root view controller is shown and my whole navigation stack is gone. I just don't want this to happen, that is, I want to remain on the 3rd view controller and also, be able to click on all tab bar items (dont want to disable any item). I know that it can be achieved through implementing tab bar controllers delegate method: shouldSelectViewController, but i dont know how??

perform a check for the currently selected viewcontroller.
if current is the same as the tab tapped, then return no in your delegate method. Think something like this is what you mean?
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if ([[tabBarController viewControllers] objectAtIndex:tabBarController.selectedIndex] == viewController)
{
return NO;
}
else
{
return YES;
}
}

Related

iPhone- How to get the main view (1st view) every time when we switching form one tab to another tab

In my iPhone app, I have used tabbarcontroller in that i have 4 tabs for four different items
In each tab i have different views navigate transaction in those.
my requirement is, when i switch form on tab to another tab by taping on tabbar item.
T*the main view (first view ) of each tab has to be appeared instead of currently working view.*
For ex:
I select tab 3 there i do some operations and navigate to some 2nd view in the third tab.
and then i secet tab 4 and then again tab3.. then the previously woriking view view 2 presents in tab3,
But i need to present 1st view at any time when i select tab 3 (Same like in any other tabs)
how to do..
Make a subclass of UINavigationController, change the class of any navigation controllers you have in your storyboard to that class, and put this code in that subclass:
-(void)viewDidDisappear:(BOOL)animated {
[self popToRootViewControllerAnimated:NO];
}
Whenever you click on another tab, this method will be called, and it will reset the navigation stack back to the root view controller.
Set your view controller or appdelegate as delegate of your tabbarcontroller. implement this delegate function
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
int selectedIndex = tabBarController.selectedIndex;
NSArray *tabbarControllers = tabBarController.viewControllers;
//Then replace tabbarControllers[selectedIndex] by your new view controller(with navigation controller)
tabBarController.viewControllers = Your replaced Array;
}

pushing onto navigation stack not within the nav controller

I have this ad class which contains an UIImageView. I've added an instance of the class to my appdelegate onto the "window" view.
Now, I want to, when the user taps the ad, push my "detailedViewController" onto the current navigation controller, which all of my tab bar items contain. I don't know if it is possible.
Perhaps, I should just add my advertisement class to every view controller for every nav controller. However, if the user pushes or changes a view controller it would reset the class.
I just want to overlay the ad once.
EDIT:
Let me rephrase, can I from the app delegate and from my object know which tab bar item is selected? If I can determine which tab bar item is selected I can point to the appropriate nav controller instance.
The easyiest way would be to present your DetailVC as a ModalView which also makes sense in semantics.
Yes, it is possible to detect which tab is selected but it is easier to use the selectedViewController-property of UITabBarController.
UIViewController *curVC = myTabBarController.selectedViewController;
if([curVC isKindOfClass:UINavigationController.class])
{
UINavigationController *nav = (UINavigationController*)curVC;
[nav push...];
}
else
{
// do sth else: go to webpage for instance
}
Whoever owns the tab bar controller can do
[myTabBarController selectedIndex];
or
[myTabBarController selectedViewController];
The first one returns the index of the selected item, the second one the actual view controller, you might be better off with the first one.

Finding out where I am in uinavigationcontroller hierarchy, or if view has been pushed?

I've got a view that sometimes appears as a pushed view of uinavigationcontroller, and sometime just as the initial view of a tab-bar item.
There's a 'Save' button on the interface which I want to make the view pop back to previous view when it's been pushed onto screen, and do nothing when it's being displayed as part of a tab bar selected screen.
In pseudo-code, I guess what I want to do is:
if view-has-been-pushed, then pop back, else do nothing
How can I tell if the view has been pushed?
As per documentation
NSArray* views = [myNavigationController viewControllers];
if (self == [views objectAtIndex:0])
{
// I am the root view
}
but as jasarien said, popViewControllerAnimated does nothing anyway if the view is already the root
Your "if view-has-been-pushed, then pop back, else do nothing" logic is easily implemented with something like:
if (self.navigationController != nil) {
// We are part of a navigation controller, so pop
}
You probably want to remove the Done button if you are not in a navigation controller? You can do the same check in viewDidLoad and show or hide the Done button there.
You could get the view controllers property from the navigation controller and compare against the first controller in the array. If the comparison returns true, then it's the root view controller, else it's been pushed.
However, if a view controller is the root view controller, calling pop should just not do anything, so you shouldn't need any extra logic.

How to know - view controller's current view in iphone

Let's have an example.
In application I have a tab bar controller.
Tab bar has two items dynamically - two view controllers.
User can select any of tab.
Suppose user selects first tab.
First view controller is already loaded.
Now he clicks on a button of First view controller.
From First View controller -> Second View controller is pushed.
Now when user taps on tab bar first item
second view is popped out.
This is done through by default by tab bar controller.
Now, If I want to check following condition
if(tab bar first item-view controller has first view controller view)
then perform this
if(tab bar first item-view controller has second view controller view)
then perform this
How to implement this logic?
If you are using a UITabBarController, you can use its selectedViewController property to know what kind of view controller is on the screen, so if you have two subclasses of view controller FirstViewController and SecondViewController you can say
if([[tabBarController.selectedVIewController isKindOfClass:[FirstViewController class]])
//... do something
else ...

Tab bar navigation

I have made an application in which I have four tab bars. I have used the below code to go to the second view in a button's click method. It goes to the second view but the second view is not showing with tab bars. How can I fix this?
- (IBAction)Existinguser:(id)sender
{
ExistingUserViewController *existingUserViewController =
[[ExistingUserViewController alloc]
initWithNibName:#"ExistingUserViewController" bundle:nil];
[self presentModalViewController:existingUserViewController animated:YES];
[existingUserViewController release];
}
Presenting a view controller modally will show that view controller above all ther other UI elements (this includes the navigation bar and tab bar, but not the status bar).
If you want to show your "second view" as the view on the second tab, then you need to set the view controller you want to use as the second tab's view controller. That way when you press the second tab, the second view controller will be shown, and the second tab will be selected and visible.
I might be mistaken, but do you have a single tab bar with four tabs and you are looking for a way to change to the second tab? You can do this with the following code:
self.tabBarController.selectedIndex = 1;
btw the code you attached to your question is displaying a new model view controller above the current view controller. This is the reason no tabbar is shown.
If you are trying to create a navigation based app using tabbar, in IB, change the tab's viewcontroller to UINavigationController. You can then use the UINavController methods to navigate between views. In case you don't want to show the navbar, just set that property in IB.
Here's a good example of how you can use tab bar with navbar