How to implement didSelectViewController

I want to catch an event when someone switches between tabs. I have the following two functions in the appdelegate file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController * uitbc = [storyboard instantiateViewControllerWithIdentifier:@"tabbarcontroller"];
    uitbc.delegate = self;
    [self.window addSubview:uitbc.view];

    return YES;
}


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

But it NSLog(@"switching");never works. Xcode issues a warning for a line uitbc.delegate = self;that says: "Passing appdelegate const__strong to an incompatible type identifier parameter."

What am I doing wrong? I just follow the accepted answer found here, except that I create my ad bar with the tabbarcontroller form:

how to get an event that switches the tab menu on iphone

Update Based on the skram suggestions, I wrote this for my application, but NSLOG (Switching) still fails:

@interface johnAppDelegate : UIResponder <UITabBarControllerDelegate>

I also updated my didFinishLauchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = self.window.rootViewController.tabBarController;
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}

, . . , didSelectViewController .

+3
3

appdelegate.h,

@interface wscAppDelegate : UIResponder <UIApplicationDelegate>

@interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>

CustomTabBarController viewDidLoad :

wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;

appdelegate.m

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

NSLog(@"hooray this works");

}
+4

, UITabBarNavigationController AppDelegate, , - viewDidLoad UITabBarController , :

-(void)viewDidLayoutSubviews {
    [ super viewDidLayoutSubviews ];
    self.delegate = self;
}

. , .

+2

You need your AppDelegate protocol compliant . See below.. UITabBarControllerDelegate

@interface YourAppDelegate : UIResponder <UITabBarControllerDelegate>
0
source

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


All Articles