Prevent tabbar from changing tab at specific index - IOS - iphone

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

Related

How to allow only single UIViewController to rotate in both Landscape and Portrait direction?

My app is only for iphone device (both iphone 4 and 5) and built to support only ios 6.
My whole app only supports portrait mode. But there is one view called "ChatView" , which i want to support both landscape and portrait modes.
I have set the required device rotations as follows -
I have also tried following code to support rotation in "ChatView" -
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
But it could not rotate that view.
I have searched a lot for this , but could not be able to find the solution for my issue.
And also in "ChatView" there are some objects like buttons, textfields whose frames are set programmaticaly. So i want to know should i have to set frames of all those objects for landscape mode also?
Please help me.
Thanks.....
Simple but it work very fine. IOS 7.1 and 8
AppDelegate.h
#property () BOOL restrictRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
viewDidLoad
[self restrictRotation:YES]; or NO
I think if you want to support just one viewcontroller rotation, it is not possible since application will follow orientations set by you in .plist file. An alternate you can follow is to support your app for both landscape and portrait, freeze all viewcontrollers rotation to portrait except for chat view.
EDIT
To subclass UINavigationController, create a new file with name e.g. CustomNavigationController and make it subclass of UINavigationController.
.h file
#import <UIKit/UIKit.h>
#interface CustomNavigationController : UINavigationController
#end
.m file
#import "CustomNavigationController.h"
#interface CustomNavigationController ()
#end
#implementation CustomNavigationController
-(BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
#end
Set the class of your UINavigationController in your main class xib as CustomNavigationController. Hope it helps ypu..
Your view controller will never rotate to any position that is not supported by the app itself. You should enable all possible rotations and then in view controllers that are not supposed to rotate put the following lines
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
In ChatView, it should be:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
If you need to change your layout after a rotation you should implement the appropriate changes to your subviews in
- (void)viewWillLayoutSubviews
Use self.view.bounds to check the current size of the view, since self.view.frame doesn't change after rotations.
for the specific viewcontroller.m you want to rotate
add this method:
- (BOOL)canAutoRotate
{
return YES;
}
then inside your AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UIViewController *currentViewController = [self topViewController];
if ([currentViewController respondsToSelector:#selector(canAutoRotate)]) {
NSMethodSignature *signature = [currentViewController methodSignatureForSelector:#selector(canAutoRotate)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:#selector(canAutoRotate)];
[invocation setTarget:currentViewController];
[invocation invoke];
BOOL canAutorotate = NO;
[invocation getReturnValue:&canAutorotate];
if (canAutorotate) {
return UIInterfaceOrientationMaskAll;
}
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController *)topViewController
{
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController
{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
Ted's answer works well with the issue mentioned by Alexander of Norway.
But I figured that issue is not happening the way Alexander explained,
When ViewController B which currently is in landscape (All
orientations enabled) returns back to ViewController A. (Portrait
only) after the user clicks on the back button,
supportedInterfaceOrientationsForWindow doesn't get called and
ViewController A ends up in landscape
Actually when ViewController B which currently is in landscape (All orientations enabled) returns back to ViewController A (Portrait only) after the user clicks on the back button, Appdelegate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
is getting called. But still root view controller is ViewController B (that rotation enabled view controller), ViewController A is not getting back to portrait orientation, since ViewController B is still returning
-(BOOL)shouldAutorotate{
return YES;
}
So when you press back button return, "shouldAutorotate -> NO" in ViewController B. Then ViewController A will come to Portrait orientation. This is what I did
#property (nonatomic, assign) BOOL canAutoRotate;
#pragma mark - Public methods
- (BOOL)canAutoRotate
{
return _canAutoRotate;
}
#pragma mark - Button actions
- (void)backButtonPressed:(UIButton *)sender {
_canAutoRotate = NO;
(...)
}
#pragma mark - Init
- (id)init{
if(self=[super init]) {
_canAutoRotate = YES;
}
return self;
}
Swift 3 Kosher Version
I've left this here just for a case somebody has the problem.
Apple's documentation for supportedInterfaceOrientations says:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns true.
In few words you have to override supportedInterfaceOrientations in root view controller so that it returns the value for its top child view controller and default value otherwise.
What you should do is checking if app supports all modes (go to Deployment info in targets General settings or Info.plist), find out the class of your root view controller. It can be generic UIViewController, UINavigationController, UITabBarController or some custom class. You can check it out this way:
dump(UIApplication.shared.keyWindow?.rootViewController)
Or any other way you like.
Let it be some CustomNavigationController. So you should override supportedInterfaceOrientations like this:
class CustomNavigationController: UINavigationController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return topViewController?.supportedInterfaceOrientations ?? .allButUpsideDown
}
}
In any view controller which should support only portrait orientation for instance override supportedInterfaceOrientations this way:
class ChildViewController: UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
Then don't forget to check if shouldAutorotate of you root view controller and topmost presented view controller already returns true. If not, add this to classes definitions:
override var shouldAutorotate: Bool {
return true
}
Otherwise supportedInterfaceOrientations will nor be called at all.
Here you go!
If you need to fix the opposite problem when only one view controller should support a bunch of orientations and others don't, make this changes to every view controller but this one.
Hope this will help.
I know this questions is very old but It needs an updated answer. The easiest and most correct way to achieve this result is to enable Portrait and Landscape in your app settings. Then add this code to your app delegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if let navigationController = self.window?.rootViewController as? UINavigationController {
if navigationController.visibleViewController is INSERTYOURVIEWCONTROLLERHERE {
return UIInterfaceOrientationMask.All
}
else {
return UIInterfaceOrientationMask.Portrait
}
}
return UIInterfaceOrientationMask.Portrait
}
Dont forget to replace "INSERTYOURVIEWCONTROLLERHERE" with your view controller.
I'm not sure about the history of this issue (now = iOS 10 timeframe), but the easiest solution was missing as I posted this in Oct. 2016.
Assuming you want this:
Supporting iOS 7 and newer only (including iOS 10)
Some view controllers should support all orientations, others should support a subset of orientations. Example of what I mean: one view controller should only support portrait, while all others should support all orientations
All view controllers must auto-rotate if they support rotation (meaning, you don't want the code that fixes this issue in your view controllers)
Support adding UINavigationControllers in XIBs/NIBs/Storyboards without having to do anything to them
... then (IMO) the easiest solution is to make a UINavigationControllerDelegate, NOT sub-class UINavigationController (which violates assumption 4 above).
When I solved this, I decided to make my first ViewController a UINavigationControllerDelegate. This view controller sets itself as the navigation controller's delegate, and returns which orientations are allowed. In my case the default is that all orientations are allowed, with portrait preferred, but in one particular case only portrait is allowed. Code below is from Swift 3 / XCode 8:
class iPhoneStartViewController: UIViewController {
var navInterfaceOrientationMask: UIInterfaceOrientationMask?
var navInterfaceOrientationPreferred: UIInterfaceOrientation! = .portrait
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
}
#IBAction func cameraButtonPressed(_ sender: AnyObject) {
if PermissionsHelper.singleton().photosPermissionGranted() == false {
self.navInterfaceOrientationMask = nil // default is: all orientations supported
self.performSegue(withIdentifier: "segueToPhotoAccess", sender: self)
} else {
self.navInterfaceOrientationMask = .portrait // this stops the next view controller from being to rotate away from portrait
self.performSegue(withIdentifier: "segueToCamera", sender: self)
}
}
}
// lock orientation to portrait in certain cases only. Default is: all orientations supported
extension iPhoneStartViewController : UINavigationControllerDelegate {
public func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
if let mask = self.navInterfaceOrientationMask {
return mask
} else {
return .all
}
}
public func navigationControllerPreferredInterfaceOrientationForPresentation(_ navigationController: UINavigationController) -> UIInterfaceOrientation {
return self.navInterfaceOrientationPreferred
}
}
Based on #iAnum's answer, I've enabled autorotate and UIViewController class detection.
This is because otherwise transitioning into and out of the "special view controller" won't correct to portrait orientation, and you'll be stuck in an unsupported orientation.
I only had one view supporting landscape, so I just hard-coded it in the custom navigation view controller:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
//Access the current top object.
UIViewController *viewController = [self.viewControllers lastObject];
//Is it one of the landscape supported ones?
if ([viewController isMemberOfClass:[SpecialViewController class]]) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
} else
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//Access the current top object.
UIViewController *viewController = [self.viewControllers lastObject];
//Is it one of the landscape supported ones?
if ([viewController isMemberOfClass:[SpecialViewController class]]) {
return interfaceOrientation;
} else
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
There's a problem popping VCs discussed here https://stackoverflow.com/a/15057537/1277350 where pressing back while in landscape won't even call the orientation methods, so you've got to hack it a bit by showing and dismissing a modal view.
And then just remember that if you want willShowViewController to fire, you need to set self.delegate = self and add the UINavigationControllerDelegate to your custom navigation controller along with the code below.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIApplication* application = [UIApplication sharedApplication];
if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
{
UIViewController *c = [[UIViewController alloc]init];
[c.view setBackgroundColor:[UIColor clearColor]];
[navigationController presentViewController:c animated:NO completion:^{
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
}
}
make a subclass of UINavigationController like so:
MyNavigationController.h
#import <UIKit/UIKit.h>
#interface MyNavigationController : UINavigationController
#end
MyNavigationController.m
#import "MyNavigationController.h"
#import "ServicesVC.h"
#implementation MyNavigationController
-(BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
if ([[self.viewControllers lastObject] isKindOfClass:[ServicesVC class]]) {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskAll;
}
#end
assuming that your viewcontroller is named: ServicesVC
Here is the answer of Alexander (https://stackoverflow.com/posts/25507963/revisions) in Swift:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
var currentViewController: UIViewController? = self.topViewController()
if currentViewController != nil && currentViewController!.canAutoRotate() {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
func topViewController() -> UIViewController? {
if UIApplication.sharedApplication().keyWindow != nil
{
return self.topViewControllerWithRootViewController(UIApplication.sharedApplication().keyWindow!.rootViewController!)
}
return nil
}
func topViewControllerWithRootViewController(rootViewController: UIViewController?) -> UIViewController? {
if rootViewController == nil {
return nil
}
if rootViewController!.isKindOfClass(UITabBarController) {
var tabBarController: UITabBarController = (rootViewController as? UITabBarController)!
return self.topViewControllerWithRootViewController(tabBarController.selectedViewController)
}
else {
if rootViewController!.isKindOfClass(UINavigationController) {
var navigationController: UINavigationController = (rootViewController as? UINavigationController)!
return self.topViewControllerWithRootViewController(navigationController.visibleViewController)
}
else {
if (rootViewController!.presentedViewController != nil) {
var presentedViewController: UIViewController = rootViewController!.presentedViewController!
return self.topViewControllerWithRootViewController(presentedViewController)
}
else {
return rootViewController
}
}
}
}
In addition, you will need to add the following snippet in AppDelegate.swift:
extension UIViewController {
func canAutoRotate() -> Bool {
return false
}}
And for ViewControllers for which you want to allow all rotations, add this function:
override func canAutoRotate() -> Bool {
return true
}
// paste this method in app deligate class
- (UIInterfaceOrientationMask)application:(UIApplication )application supportedInterfaceOrientationsForWindow:(UIWindow )window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [_moviePlayerController class]])
{
if (self.window.rootViewController.presentedViewController)
return UIInterfaceOrientationMaskAll;
else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}
I had the same situation. So I subclassed UINavigationController into CustomNavigationController, and inside this CustomNavigationController, I wrote
#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )
#pragma mark - Rotation
#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
#endif
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;;
}
#endif
I used this CustomNavigationController instead of existing NavigationController.
Then inside the view controller that you have to display in LandScape Orientation say LandScapeView, I wrote
#pragma mark - Rotation
#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight | toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
#endif
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
#endif
Inside CustomNavigationController, I presented this view controller, not pushed into Navigation Stack. So the LandScapeView appeared in LandScape Orientation.
LandScapeView *graph = [[LandScapeView alloc]init....];
[self presentViewController:graph animated:YES completion:nil];
I did not change anything in the Supported Interface Orientation in Project Settings.
If the App is supporting from IOS7 to IOS9 use this code for Orientation:
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
if([AppDelegate isPad]) return UIInterfaceOrientationMaskAll;
else return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

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;
}

tabBarController:shouldSelectViewController method doesn't fire

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;