Topic : How to use TabBar in viewbase application

本文详细介绍了如何在iOS应用中不使用UITabBarController的情况下,手动实现TabBar的功能。包括在Interface Builder中设置TabBar、连接代理、响应选择事件、切换视图等内容。

To use a tab bar without UITabBarController, you'll need to implement some of the controller functionality in your own code:

1) In IB, add a tab bar to your content view and add as many tab items as you want;
2) Ctrl-drag from the tab bar to the view controller (e.g. File's Owner), to connect the delegate outlet of the tab bar to the controller;
3) Select each tab bar item and set its Tag number in the Attributes Inspector: 1, 2, 3, ...
4) in Xcode, open the @interface file for the controller class and adopt the UITabBarDelegate Protocol:
@interface BarTestViewController : UIViewController <UITabBarDelegate> {
}
@end

5) In the @implementation for the controller class add this delegate method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
	NSLog(@"didSelectItem: %d", item.tag);
}

6) Test the app to make sure the selection of each tab item is correctly logged in the Console. E.g.:
[Session started at 2009-07-31 01:11:18 -0700.]
2009-07-31 01:11:22.770 BarTest[22339:20b] didSelectItem: 1
2009-07-31 01:11:25.930 BarTest[22339:20b] didSelectItem: 2
2009-07-31 01:11:27.274 BarTest[22339:20b] didSelectItem: 3

Once your tab bar is working, you'll need to add more xibs and the code to switch the view based on the tab item selected.

Make a new view controller subclass for each tab. Also add one more View XIB file (New File->iPhone OS->User Interfaces) for each tab. Change the Class of each File's Owner to the corresponding subclass and connect each view to the File's Owner view outlet. Also size each view to account for the bar (you can use Simulated Metrics in the attribute inspector to do this).

To the @interface of the top view controller add an IBOutlet for the tab bar along with ivars for each of the new view controllers. Also add a view controller ivar named currentViewController which you can use to keep track of the currently selected controller:
@property (nonatomic, retain) IBOutlet UITabBar *myTabBar; // <-- connect to the tab bar in IB
@property (nonatomic, retain) UIViewController *tab1ViewController;
@property (nonatomic, retain) UIViewController *tab2ViewController;
@property (nonatomic, assign) UIViewController *currentViewController;

When you switch views, use insertSubview:belowSubview: to keep the tab bar visible. E.g. this code could be in a switch statement in tabBar:didSelectItem::
		case 2:
			if (tab2ViewController == nil) {
				self.tab2ViewController =
					[[Tab2ViewController alloc] initWithNibName:Tab2View bundle:nil];
			}
			[self.view insertSubview:tab2ViewController.view belowSubview:myTabBar];
			if (currentViewController != nil)
				[currentViewController.view removeFromSuperview];
			currentViewController = tab2ViewController;			
			break;

The above should get you started in the right direction. Is there enough here so you can code the rest by yourself?

- Ray 

MacBook   Mac OS X (10.5.5)     
MUsman 
 
Posts: 145 
From: Pakistan
Registered: Feb 11, 2009
 Re: How to use TabBar in viewbase application 
Posted: Jul 31, 2009 5:00 AM    in response to: RayNewbie
 

thankz dude. 
this worked for me.
Muhammad Usman Aleem 

  iPhone OS 3.0     
ssalminen 

Posts: 5 
From: Helsinki, Finland
Registered: Dec 8, 2009
 Re: How to use TabBar in viewbase application 
Posted: Dec 8, 2009 1:33 PM    in response to: RayNewbie
 

Thank you very much raynewbie, you rock!

I posted complete example here. Use 
svn co http://pymbian.svn.sourceforge.net/svnroot/pymbian/stuff/testtab_raynewbie/ to check it out.

It has following structure:
AppDelegate
- testtabViewController: UIViewController
--  tabviewtest: UIViewController <UITabBarDelegate> (this uses your sample code)
--- UITabBar
---- Item1: UITabBarItem
---- Item2: UITabBarItem

I also added that you specify the starting tab when tab bar is shown in first time. Seehttp://pymbian.svn.sourceforge.net/svnroot/pymbian/stuff/testtab_raynewbie/Classes/tabviewtest.m
- (void)viewDidLoad {
    [super viewDidLoad];
    [myTabBar setSelectedItem:[myTabBar.items objectAtIndex:0]];
    [self activateTab:1]; // this is same as your didSelectItem at post above
}

I hope this is not too kludge version, and keeps working on future version of iPhone SDK.

Message was edited by: ssalminen

Message was edited by: ssalminen 

      
RayNewbie 


Posts: 2,317 
From: Watsonville, CA
Registered: Aug 15, 2008
 Re: How to use TabBar in viewbase application 
Posted: Dec 8, 2009 8:32 PM    in response to: ssalminen
 

Thanks so much for letting me know that code was useful to you!! It looks like you did a nice job of turning the outline into a complete sample app. Maybe the project could be titled "Tab Bar Controller Lite": For use when limited tab bar functionality is acceptable in exchange for the ability to place the controller below the root.

I hang out here primarily because I enjoy teaching (and of course learning a lot in the process!). I don't often know whether my instructions or sample code were useful to anyone besides the owner of the context. It's an honor to see what you built from my example at your site.

- Ray 

MacBook   Mac OS X (10.5.5)     
tim_d 
 
Posts: 10 
From: Sheffield, UK
Registered: Sep 17, 2006
 Re: How to use TabBar in viewbase application 
Posted: Mar 23, 2010 3:34 PM    in response to: RayNewbie
 

Thanks for posting this - it's took me the best part of a day hacking around trying to get a tabbar inside a navigation controller, and this code had it sorted in 20 mins! 

15" Unibody MBP / 24" iMac   Mac OS X (10.6.2)     
RayNewbie 


Posts: 2,317 
From: Watsonville, CA
Registered: Aug 15, 2008
 Re: How to use TabBar in viewbase application 
Posted: Mar 23, 2010 5:34 PM    in response to: tim_d
 

You're very welcome, Tim!! I really appreciate your taking a moment to let me know. In fact, these days it's great to find some of my code that didn't become obsolete in 8 months. I think that might be close to 5 dog years, 10 computer years, and maybe 40 Apple years.

All the best,
Ray 

iMac   Mac OS X (10.5.8)     
inbaebae 

Posts: 1 
From: CA
Registered: Apr 14, 2010
 Re: How to use TabBar in viewbase application 
Posted: Apr 14, 2010 3:13 PM    in response to: RayNewbie
 

Hello Everyone,

This thread has been super helpful for coding a part of my application. 
So basically, the entire application is navigation based. 
The home screen is a table view with a list of sub applications with each cell loading the home screen view of a sub application. 

I have used the examples above to create one of the sub application as a UIViewController, with a tab bar below the view. For each tab, I have a seperate class (UIViewController) that represents each tab's view. All of these is very similar to the example project code posted above.

Now, for one of the tab views it contains a search bar and a tableview. I have the searching and displaying of the results in the cell's of the tableview all working currently. The problem is, when I click on the cell, I want to be able to push the new view on top of the current view (search bar, table view) with the tab bar below. This is the method that I have implemented below which is called when clicking on a cell in the tableview. However, nothing changes when I click on the cell. I feel the problem is that I have to do something along the lines similar to the activatetab method implemented in the example. 

 

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"Cell clicked!"); 
UIViewController *newView = [[tab1DetailView alloc] initWithNibName:@"tab1DetailView" bundle:nil];

[self.navigationController pushViewController:newView animated:YES];

}

I will explain three following classes..

tab1ViewController //the view controller for the first tab
tab1DetailView //the detailed view for a search result. 
CRMViewController //the sub app which is the sub application loading the main view with the tab bar, view and nav bar at the top. 

I am not sure if this is clear enough but if someone could lead me in the right direction, that would be much appreciated! If something needs to described in a clearer fashion, I can do that as well. Thank you! 

标题基于Python的汽车之家网站舆情分析系统研究AI更换标题第1章引言阐述汽车之家网站舆情分析的研究背景、意义、国内外研究现状、论文方法及创新点。1.1研究背景与意义说明汽车之家网站舆情分析对汽车行业及消费者的重要性。1.2国内外研究现状概述国内外在汽车舆情分析领域的研究进展与成果。1.3论文方法及创新点介绍本文采用的研究方法及相较于前人的创新之处。第2章相关理论总结和评述舆情分析、Python编程及网络爬虫相关理论。2.1舆情分析理论阐述舆情分析的基本概念、流程及关键技术。2.2Python编程基础介绍Python语言特点及其在数据分析中的应用。2.3网络爬虫技术说明网络爬虫的原理及在舆情数据收集中的应用。第3章系统设计详细描述基于Python的汽车之家网站舆情分析系统的设计方案。3.1系统架构设计给出系统的整体架构,包括数据收集、处理、分析及展示模块。3.2数据收集模块设计介绍如何利用网络爬虫技术收集汽车之家网站的舆情数据。3.3数据处理与分析模块设计阐述数据处理流程及舆情分析算法的选择与实现。第4章系统实现与测试介绍系统的实现过程及测试方法,确保系统稳定可靠。4.1系统实现环境列出系统实现所需的软件、硬件环境及开发工具。4.2系统实现过程详细描述系统各模块的实现步骤及代码实现细节。4.3系统测试方法介绍系统测试的方法、测试用例及测试结果分析。第5章研究结果与分析呈现系统运行结果,分析舆情数据,提出见解。5.1舆情数据可视化展示通过图表等形式展示舆情数据的分布、趋势等特征。5.2舆情分析结果解读对舆情分析结果进行解读,提出对汽车行业的见解。5.3对比方法分析将本系统与其他舆情分析系统进行对比,分析优劣。第6章结论与展望总结研究成果,提出未来研究方向。6.1研究结论概括本文的主要研究成果及对汽车之家网站舆情分析的贡献。6.2展望指出系统存在的不足及未来改进方向,展望舆情
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值