Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

objective c - How to implement didSelectViewController

I want to catch the event when someone switches between tabs. I have the following two function in my 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 the NSLog(@"switching"); never fires. The xcode issues a warning for the line uitbc.delegate = self; saying "Passing appdelegate const__strong to parameter of incompatible type id".

What am I doing wrong? I'm just following the accepted answer found here, except i'm instantiating my tabbarcontroller form story board:

how to get the event that switch tab menu on iphone

Update Based on skram's suggestion, I wrote this for my appdelegate but the NSLOG(Switching) still doesn't fire:

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

Good thing is that nothing crashes. I also no longer et the warning about incompatible types. But still, didSelectViewController doesn't fire.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

in my appdelegate.h file, I changed the line

@interface wscAppDelegate : UIResponder <UIApplicationDelegate>

to

@interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>

Then in my CustomTabBarController in the viewDidLoad function i added these lines:

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

Then in appdelegate.m file, I added this function

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

NSLog(@"hooray this works");

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...