DidSelectViewController method is not called (with storyboard)

I have two versions of an ios5 tabbed application created using a storyboard, and one using xib files. The storyboard version does not call the UITabBarControllerDelegate didSelectViewController (xib version) method. Something (I think) is missing from the storyboard, but I don't know what. Another way to UITabBarController question may be as follows: how can I refer to the UITabBarController object created by the storyboard?

Thank you for your help.

EDIT: tab panel controller delegate installed:

In AppDelegate.h:

 @interface MyAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> @property (strong, nonatomic) UITabBarController *tabBarController; 

In AppDelegate.m:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tabBarController.delegate = self; return YES; } 

Then in the AppDelegate.m delegation method:

 - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"Got Here"); } 

The NSLog output never appears. It seems that the problem is that I am incorrectly referring to the tab bar controller object that was created using the storyboard. How to do it?

+3
source share
2 answers

I had this problem.

If you are not using storyboards, setting the UITabBarController delegate to AppDelegate is the way to go. However, with Storyboards , AppDelegate has no idea where the tabBarController is at startup. You might think of subclassing tabBarController and add a delegate method:

 (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { } 

... would be enough. But it is annoying.

I needed to know when the user clicked the tab button. I needed to know this more, what I needed to know was that the viewController " - (void)viewWillDisappear:(BOOL)animated {} " method was running.

I decided to make my UITabBarController my delegate. It seemed silly to me, but I did the following ...

 #import <UIKit/UIKit.h> @interface PlumbsTabBarController : UITabBarController <UITabBarControllerDelegate> @end 

And then the following is written in my viewDidLoad method:

 [self setDelegate:self]; 

Which allowed me to run the delegation methods of the tab bar.

Crazy or what?

Good - now I am editing this answer, because, despite the fact that everything is correct above, where the navigationController used, it is selected every time the tabBarButton touched, the tabBarButton delegate didSelectViewController will be when you try before NSLog(@"%@", viewController); only shows that you have selected the UINavigationController class?

So, the general solution, just to add extra complexity, is to subclass the UINavigationController for each viewController that you want to control (do something) when you touch tabBarButton .

It works for me anyway. And, if someone can skip the aforementioned dribbling, they can find a useful aspect - and that's enough for me - seeing how I find this site completely useful.

+8
source

Put [self setDelegate:self]; to your ViewDidLoad or somewhere where the object is initialized

+6
source

Source: https://habr.com/ru/post/951502/


All Articles