我目前有一个带有标签栏和多个视图控制器的 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