現在、タブバーと複数のビューコントローラーを備えた iPhone アプリケーションがあります。すべてのビューは Interface Builder で設計されています。ビューコントローラーから現在選択されているタブバーのインデックスを取得できるようにしたいのですが、何らかの理由でこのプロパティが (null) を返します。
ビューコントローラーの viewDidLoad 関数で次のように呼び出しました。
self.tabBarController.selectedIndex
これを行う正しい方法は何ですか?
AppDelegate クラスのコードで更新されました。
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