在Xcode中以编程方式按下UITabBar按钮

时间:2011-10-27 14:47:07

标签: iphone objective-c uitabbarcontroller tabbar

我使用下面的代码制作了一个UITabBarController:

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

1 个答案:

答案 0 :(得分:4)

你可以这样做:

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

或者这个:

self.rootController.selectedViewController = oneOfTheViewControllersInTheTabController;

有关详细信息,请参阅UITabBarController reference page