How can I create a tab view programmatically on iOS

此生再无相见时 提交于 2019-12-06 02:49:06

问题


For an iPhone app how can I create a tab view programmatically, preferably in Objective-C?


回答1:


It's quite simple to create a UITabBar via the UITabBarController. The following example should work within your AppDelegate class.

App Delegate Interface

Firstly, within the interface, we'll define our UITabBarController.

UITabBarController *tabBarController;

App Delegate Implementation

Then, within the implementation file's application:didFinishLaunchingWithOptions: method, we'll then initialise our tab bar controller.

// Initialise our tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];

Next, you need to create the view controllers that you want to add to the tab bar controller. We'll need to add some information into these to set the tab's title/icon, but I'll come back to that at the end.

// Create your various view controllers
UIViewController *testVC = [[TestViewController alloc] init];
UIViewController *otherVC = [[OtherViewController alloc] init];
UIViewController *configVC = [[ConfigViewController alloc] init];

As the setViewControllers:animated: method requires an array of view controllers, we'll add our view controllers to an array and then release them. (As the NSarray will retain them.)

// Put them in an array
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil];
[testVC release];
[otherVC release];
[configVC release];

Then simply provide the UITabBarController with the array of view controllers and add it to our window.

// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers animated:NO];

// Put the tabBarController's view on the window.
[window addSubview:[tabBarController view]];    

Finally, make sure you call [tabBarController release]; within your dealloc method.

View Controller Implementation

Inside each of your view controllers, you'll also want to set the title and icon for the tab within the init method as follows:

// Create our tab bar item
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:@"YOUR TITLE"];



回答2:


This is how we have to create tabbar programmatically

UINavigationController *BandNavigationController3;
AudienceSettingsViewController *audienceSettingsViewView =[[AudienceSettingsViewController alloc]initWithNibName:@"AudienceSettingsViewController" bundle:nil];
BandNavigationController3 = [[UINavigationController alloc]initWithRootViewController:audienceSettingsViewView];
BandNavigationController3.tabBarItem.title = @"Settings";
BandNavigationController3.tabBarItem.image = [UIImage imageNamed:@"settings.png"];

[BandNavigationController3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:4];
BandNavigationController3.navigationBar.hidden = YES;

[bandTabBarArray addObject:BandNavigationController3];
[BandNavigationController3 release];
[audienceSettingsViewView release];

[tabBarController setViewControllers:bandTabBarArray]; 
[bandTabBarArray release];


来源:https://stackoverflow.com/questions/5920305/how-can-i-create-a-tab-view-programmatically-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!