Charles Yeung
Charles Yeung

Reputation: 38803

Setting the bottom bar as a tab bar programmatically in Objective-c

In IB, I can set the bottom bar as a tab bar for a view as below screen shot, but how can I set it by code in the implement(.m) file?

Thanks

enter image description here

Upvotes: 0

Views: 1304

Answers (1)

PengOne
PengOne

Reputation: 48398

Create an NSArray of the UIViewControllers that you want to use. Then instantiate a UITabBarController and set the viewControllers property to this array. Then add the view of the tabBarController to the window. All this should be done in the AppDelegate.m file. For example:

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   

    UIViewController *vc1 = [[UIViewController alloc] init];
    UIViewController *vc2 = [[UIViewController alloc] init];
    CustomViewController *vc3 = [[CustomViewController alloc] init];

    NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, vc3, nil];
    [vc1 release]; [vc2 release]; [vc3 release];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:viewControllers];

    [window addSubview:[tabBarController view]];
    [window makeKeyAndVisible];

    return YES;
}

Upvotes: 3

Related Questions