How do I recognize if the tab is changed?

Discussion in 'iPhone/iPad Programming' started by ArtOfWarfare, Aug 20, 2008.

  1. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    OK, I want my game to pause if the player selects one of the other tabs.

    Here's the code I tried...

    first off, I made outlets for all the tabs and the tab bar those have all been connected in Interface Builder.
    Code:
    	IBOutlet	UITabBar		*tabBar;
    	IBOutlet	UITabBarItem		*optionsTab;
    	IBOutlet	UITabBarItem		*numbersTab;
    	IBOutlet	UITabBarItem		*scoresTab;
    	IBOutlet	UITabBarItem		*instructionsTab;
    I've got this to set my controller object as the delegate of all the tabs:
    Code:
    - (void) awakeFromNib
    {
    	tabBar.delegate = self;
    	optionsTab.delegate = self;
    	numbersTab.delegate = self;
    	scoresTab.delegate = self;
    	instructionsTab.delegate = self;
    }
    There's an error for each one of the tab items...
    I have tried commenting them out, and the compiler will run without errors, but it still doesn't work.

    I also have:

    Code:
    @interface Controller : NSObject <UITextFieldDelegate, [b]UITabBarDelegate[/b]>
    In my header so that I wouldn't get the compiler error that I was missing the protocols.

    And finally I have this:
    Code:
    - (void) tabBar: tabBar didSelectItem: tabItem
    {
    	NSLog(@"Recognises that a tab item was selected.");
    	[self pauseGame];
    }
    I get the following warning here:
    The NSLog message isn't ever posted meaning something prior to the code for pauseGame isn't working.
     
  2. macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    UITabBarItems do not have delegates. Look at the documentation for UITabBar and UITabBarDelegate. Your delegate method is not defined correctly.
     
  3. thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    ...

    OK...

    I got rid of the outlets and the messages to set the delegates of the tab items (but not the tab bar.)

    I changed other bit to:

    Code:
    - (void) tabBar: (UITabBar *) tabBar didSelectItem: (UITabBarItem *) item
    {
    	NSLog(@"Recognizes that a tab item was selected.");
    	[self pauseGame];
    }
    which got rid of the warning message.

    But it still doesn't work. The NSLog message is never sent.

    I also tried:

    Code:
    - (void) tabBar: (UITabBar *) tabBar didSelectItem: (UITabBarItem *) optionsTab
    {
    	NSLog(@"Recognizes that a tab item was selected.");
    	[self pauseGame];
    }
    with the optionsTab outlet set up again, but it still didn't work.

    Edit: OK, I found out what I actually needed was the TabBarControllerDelegate's

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

    method.

    Now it all works properly.
     

Share This Page