这里有两个方面....
第一个方面(UITabBarController):
在这里,您可以向 UITabBarController 添加多个类(即 Viewcontrollers),如前面的答案所示......
第二个方面(UITabbar):
在这里您可以在任何视图控制器中添加 Tabbar...对于此 Tabbar 您可以添加多个 UITabBar Items..并且您可以获得单独的 Tabbar Item Action.....
UITabBar
请参见下面的代码
---------> 添加UITabBar
UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
myTabBar.delegate=self; //here you need import the protocol <UITabBarDelegate>
[self.view addSubview:myTabBar];
---------> 向UITabBar添加项目
NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Item1" image:[UIImage imageNamed:@"Item1image.png"] tag:0];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1 ];
UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2 ];
[tabBarItems addObject:tabBarItem1];
[tabBarItems addObject:tabBarItem2];
[tabBarItems addObject:tabBarItem3];
myTabBar.items = tabBarItems;
myTabBar.selectedItem = [tabBarItems objectAtIndex:0];
---------> 在委托方法中获取项目操作
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger selectedTag = tabBar.selectedItem.tag;
NSLog(@"%ld",(long)selectedTag);
if (selectedTag == 0) {
//Do what ever you want here
} else if(selectedTag == 1) {
//Do what ever you want
} else { //if(selectedTag == 2)
//Do what ever you want here
}
}
更新:(Swift 2.0)
1.第一方面(UITabBarController):
这将允许我们将UIViewControllers 配置为tabbar 在UITabBarController 中的项目。
以下是 UITabBarController 的基本方法:
--------->将UITabBarController添加到appdelegate中的窗口
let tabBarController = UITabBarController(nibName: nil, bundle: nil)
tabBarController.delegate = self // Need to import 'UITabBarControllerDelegate'
self.window!.rootViewController = tabBarController;
self.window?.makeKeyAndVisible();
---------> 配置 UIViewControllers
let firstViewController = UIViewController()
firstViewController.title = "FirstVC"
firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let firstNavController: UINavigationController = UINavigationController(rootViewController: firstViewController)
let secondViewController = UIViewController()
secondViewController.title = "SecondVC"
secondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let secondNavController: UINavigationController = UINavigationController(rootViewController: secondViewController)
let thirdViewController = UIViewController()
thirdViewController.title = "ThirdVC"
thirdViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let thirdNavController: UINavigationController = UINavigationController(rootViewController: thirdViewController)
let forthViewController = UIViewController()
forthViewController.title = "ForthVC"
forthViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
let forthNavController: UINavigationController = UINavigationController(rootViewController: forthViewController)
---------> 将 UIViewControllers 附加到 UITabBarController
tabBarController.viewControllers = [firstNavController, secondNavController, thirdNavController, forthNavController]
--------->在'UITabBarControllerDelegate'中接收事件
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
print("didSelectViewController: \(viewController.title) ?")
}
2. 第二个方面(UITabBar):
这将允许我们创建tabbar 项目。我们可以在委托方法中获取单个项目的事件
以下是 UITabBar 的基本方法:
---------> 将 UITabBar 添加到视图
let frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 49, CGRectGetWidth(self.view.frame), 49)
let tabBar: UITabBar = UITabBar(frame: frame)
tabBar.delegate = self // Need to import 'UITabBarDelegate'
self.view.addSubview(tabBar)
--------->准备TabbarItems
let tabBarItem1 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let tabBarItem2 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let tabBarItem3 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let tabBarItem4 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
--------->将UITabBarItems添加到UITabBar
tabBar.items = [tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4]
tabBar.selectedItem = tabBarItem1
--------->在'UITabBarDelegate'中接收事件
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let selectedTag:Int = (tabBar.selectedItem?.tag)!
print(selectedTag)
switch selectedTag {
case 0:
print("tabBarItem1")
//Do what ever you want here
case 1:
print("tabBarItem2")
//Do what ever you want here
case 2:
print("tabBarItem3")
//Do what ever you want here
default:
print("tabBarItem4")
//Do what ever you want here
}
}