tabBarController:shouldSelectViewController method doesn't fire - iphone

I have read the Apple docs - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1 about creating TabBar programmatically. I want to detect the TabBar selection so I have used following delegate methods. I am not sure why but these methods don't get fired when I change the Tabs on my iPhone. Could anyone please provide some thought on what's going wrong here. It would be really helpful. Thanks.
- (BOOL)tabBarController:(UITabBarController *)tbController shouldSelectViewController:(UIViewController *)viewController
{
if (viewController == [tbController.viewControllers objectAtIndex:3] )
{
// Enable all but the last tab.
return NO;
}
return YES;
}
- (void)tabBarController:(UITabBarController *)tbController didSelectViewController:(UIViewController *)viewController {
if (viewController == [tbController.viewControllers objectAtIndex:self.appTabs.count] )
{
//do some action
}
}

Did you forget to set the delegate when you created the UITabBarController?
someTabBarController.delegate = self;

Related

Prevent tabbar from changing tab at specific index - IOS

Thanks for reading my question.
I'm trying to implement a popup menu when a user clicks the tab with the index of 4. So I'm trying to prevent the tabbar from switching viewcontroller when index 4 is pressed.
Here is my code:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if(viewController == [tabBarController.viewControllers objectAtIndex:4]){
NSLog(#"NO");
return NO;
}else{
NSLog(#"YES");
return YES;
}
}
I've implemented the UITabBarControllerDelegate and self.delegate = self; in the viewDidLoad and it works but just one time.
When I click the index 4 tab the menu shows and the tabbar doesn't switch view (GREAT), but when I click it again the view changes even if I get the Log "NO".
What could be the problem here?
Thanks you for any suggestions!
SOLVED
Thanks to Kasaname's answer below I solved it by adding selectedindex and set it to a flag index (prevtab). I change the prevtab to the index of the last selected tab, exept for when the user selects index 4.
My final code:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if(viewController == [tabBarController.viewControllers objectAtIndex:4]){
self.selectedIndex = prevTab; //only change in this method
return NO;
}else{
return YES;
}
}
This is how you can stop/prevent Tabbar items to switch your tab on tabbar item click
For Swift 3.0
Make sure you have implemented UITabBarControllerDelegate and set UITabbarController's delegate to self
then override this delegate in your controller
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController == tabBarController.viewControllers?[2] {
return false
} else {
return true
}
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
} else if (tabBarController.selectedIndex == 1) {
} else if (tabBarController.selectedIndex == 2) {
}
}
why dont u use this delegate
Use this delegate it will work i suppose

Compare instances or objects of same UIViewController class

I have created two instances of a MasterViewController derived from UIViewController class
_masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController_iPhone" bundle:nil];
// second instance with same class and duplicate nib view
_favItemMasterVC = [[MasterViewController alloc] initWithNibName:#"favMasterViewController_iPhone" bundle:nil];
Both the MasterViewController_iPhone & favMasterViewController_iPhone view are same.
Now I want to check which of the UIViewController is currently selected(eg:on tabbar).
How can i find the difference between both objects?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[_favItemListMasterVC class]]
{ // it is always called in both cases}
isMemberOfClass: // is also not working
How to check the difference?
Not sure I have understand what are you doing, but if _favItemListMasterVC and _masterViewController are pointing to the same VCs added to the UITabBar, you can check it simply comparing pointers
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController == _favItemListMasterVC)
{
//the visible view controller is _favItemListMasterVC
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController == _masterViewController)
{
}
else if (viewController == _favItemMasterVC)
{
}
}
I think you can use tag to check which is which. Tag is property of a UIView Set the tag value in the two xib files. And check the tag using code.
To compare objects you can also use:
if([viewController isEqual:_favItemMasterVC])

Disable tap on current Tab (UITabBarController) iPhone App

Currently, Tapping on the same Tab (in which user is working), The App moves to the very first page of that Tab.
I want to disable the tap event on the Tab in which user is working currently.
Any Hint?
You tried tabBarController:shouldSelectViewController: delegate method? I hope that should help you.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
id currentViewController = tabBarController.selectedViewController;
return (viewController != currentViewController);
}
If all the view controllers of the tab bar controller are UINavigationControllers, you should do it like this.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
id nextVC = [(UINavigationController *)viewController topViewController];
id currentVC = [(UINavigationController *)tabBarController.selectedViewController topViewController];
return (nextVC != currentVC);
}
For Swift 4 the delegate method looks like this:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return viewController != tabBarController.selectedViewController
}
use like below it will work
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if(self.tabBarController.selectedIndex==[[self.tabBarController viewControllers] indexOfObject:viewController])
return NO;
else
return YES;
}

iphone application for tabbar

How can I defined and check the alreadySelectedSpecificTab and viewControllerNotToAllow in
my application. anyone give me one example for that.
Actuly I would like to do something. like. When second tab is selected that time if we select second tab is not selected only remaing tab is to be selected.
thats why I used the following code.
please reply
- (BOOL)tabBarController:(UITabBarController *)tabBarControllers shouldSelectViewController:(UIViewController *)viewController
{
if(alreadySelectedSpecificTab)
{
if([viewController isEqual:viewControllerNotToAllow])
return NO;
}
return YES;
}
Create some properties in your class
and keep track of what you want denied and what you dont want denied.
id currentlySelected; //This will hold the address of the selected view
id dontAllowSelection; //This will hold the address of the Denied view
- (BOOL)tabBarController:(UITabBarController *)tabBarControllers shouldSelectViewController:(UIViewController *)viewController
{
if (dontAllowSelection != nil && //If it is nil, we will skip it.
dontAllowSelection == viewController) //If the selected view is Denied return NO
{
return NO;
}
currentlySelected = viewController;
if (currentlySelected == someViewIWantToDisableOtherFor) //Any logic can go here to enable the Denied View.
{
dontAllowSelection = anotherViewIWantDisabled; //Set my denied view.
}
else
{
dontAllowSelection = nil; //Unsed the Denial.
}
return YES;
}

How to specify a group when displaying an ABPeoplePickerNavigationController

How do you specify a group when initially displaying an ABPeoplePickerNavigationController (so it doesn't automatically display "All Contacts")?
Yeah, I do. I had to make it work.
Set your class as the delegate of the people picker (pp.delegate = self;)
Then implement:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([navigationController.viewControllers count] > 1) {
navigationController.delegate = nil;
[navigationController popViewControllerAnimated:NO];
}
}
It seems to work best with animation off, but still works with it on but sort of goofy. Only tested on simulator.
D