views:

813

answers:

2

I currently have an iPhone application with a tabbar and multiple viewcontrollers. All the views are designed in Interface Builder. I'd like to be able to get the currently selected index of the tabbar from the viewcontroller, but for some reason this property returns (null).

I called the following in the viewDidLoad function of my viewcontroller:

self.tabBarController.selectedIndex

What would be the correct way to do this?

Updated with the code of the AppDelegate class.

MyAppDelegate.h

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

MyAppDelegate.m:

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  [window addSubview:tabBarController.view];
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end
A: 

You should have a pointer to your tabbar in your appDelegate class. Your view has no tabbar, so you recieve nil from [self.tabBarController selectedIndex].

Morion
How can I do that? There is a pointer to tabBarController in the class. I've update the question with the code of the appDelegate class.
mensch
myAppDelegate* getAppDelegate(){ return (myAppDelegate*)[UIApplication sharedApplication].delegate;}you can add this method to your appdelegate class and call your tabbar by getAppDelegate().tabBarController
Morion
Is this the same as: MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; NSLog(@"%@", appDelegate.tabBarController.selectedIndex); Because this returns (null) as well, I'm afraid. And the application crashes when switching to another tab section
mensch
A: 

I think I've got it. Using the following returns the correct index:

  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%d", appDelegate.tabBarController.selectedIndex);

The reason the application was crashing was the fact that I used %@ instead of %d in the NSLog part. I could have sworn I tried %d before, strange...

The index is now returned, but only once. After you tap the tab section an index number is returned, but when you tap another section again no number is printed. Probably because the view has already been loaded once. Is there any way to work around this?

mensch
Use a different method? viewDidLoad is the callback that the system uses to tell your view controller that its view has just been loaded (whether from a nib file, or the code in loadView, or whatever). It may be called multiple times over the life of the controller, but only if the view was unloaded at some point in between (say, by a low-memory warning). Perhaps you're looking for something like viewWillAppear: ?
Sixten Otto
Also, this kind of clarification should really be an edit to the question, not an answer.
Sixten Otto
Sorry about that, I'll edit the root question in the future from now on.Thanks for the viewDidLoad pointer, will look into that.
mensch
viewWillAppear is exactly what I was looking for! Sorry for such an obvious question, but the scope of the SDK is quite mesmerising at the moment. Thanks!
mensch