PDA

View Full Version : How do I recognize if the tab is changed?




ArtOfWarfare
Aug 20, 2008, 01:55 PM
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.
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:
- (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...
error: request for member 'delegate' in something not a structure or union

I have tried commenting them out, and the compiler will run without errors, but it still doesn't work.

I also have:

@interface Controller : NSObject <UITextFieldDelegate, UITabBarDelegate>

In my header so that I wouldn't get the compiler error that I was missing the protocols.

And finally I have this:
- (void) tabBar: tabBar didSelectItem: tabItem
{
NSLog(@"Recognises that a tab item was selected.");
[self pauseGame];
}
I get the following warning here:
warning: conflicting types for '-(void)tabBar: (id)tabBar didSelectItem: (id)tabItem'

The NSLog message isn't ever posted meaning something prior to the code for pauseGame isn't working.



admanimal
Aug 20, 2008, 02:13 PM
UITabBarItems do not have delegates. Look at the documentation for UITabBar and UITabBarDelegate. Your delegate method is not defined correctly.

ArtOfWarfare
Aug 20, 2008, 02:47 PM
...

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:

- (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:

- (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.