2011-10-27 72 views
2

編程按下中的UITabBar一樣按鈕,我用下面的代碼做出的UITabBarController:在Xcode

AppDelegate.h

IBOutlet UITabBarController *rootController; 
... 
    @property (nonatomic, retain) IBOutlet UITabBarController *rootController; 

內AppDelegate.m

@synthesize rootController; 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [self.window addSubview:rootController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

現在我需要知道如何在AppDelegate內實現此方法:

- (void)SwitchToTab:(int)index{ 
    //go to tabview 1 or 2 ... 
    } 
+0

你試過 [rootController setSelectedItem:[rootController.items objectAtIndex:index]]; ? – Mat

回答

4

你可以這樣做:

self.rootController.selectedIndex = 2; // or whatever index you like 

或本:

self.rootController.selectedViewController = oneOfTheViewControllersInTheTabController; 

有關詳細信息,請參閱UITabBarController reference page