http://www.iphonedevcentral.com/create-uitabbarcontroller/
Create a UITabBarController from scratch
Tab bar based apps are probably as common as table-based apps and it’s even more common to see them combined. That’s what we’re going to do in this tutorial.
There is a very easy way of creating a tab bar application. In fact, it’s so easy it requires no work whatsoever. When you choose to create a new iPhone application in Xcode, one of the options is
Tab Bar Application.
Just the bare-bones template provided by Apple gives you a fully-functioning app with two tabs. That is not what this tutorial will be about. We are going to create a tab bar controller programatically. It’s really a lot easier than most people think.
Prerequisites
This tutorial is a continuation of my
previous tutorials
so if you haven’t followed them, it’s a good time to catch up.
We’re going to be starting off from a slightly modified version of the previous source code. You can download the primer code for this tutorial here:MyDVDLibrary04Primer
. The only changes made were some name changes (DetailViewController
became
DvdInfoViewController
) and I organized all classes into groups.
Also, I’ve recently upgraded to OS X Snow Leopard + Xcode 3.2 with iPhone SDK 3.0 (not 3.1 yet) so the project was compiled on that platform.
1. Create DetailViewTabBarController class
We’re going to add a view controller that will create a tab bar controller with three tabs: Info, Stats and Borrowers. Each of the tabs will be a separate, self-contained view controller. We already created one of them,
DvdInfoViewController
, which was previously called
DetailViewController
. The reason I renamed it is that the real detail view controller will now be our tab bar controller.
In Xcode, right-click on the
View Controllers
folder from the the
Groups & Files
panel on the left and choose
Add -> New File
. Choose
UIViewController subclass
from the
Cocoa Touch Class section
and click
Next
. Name the file
DetailViewTabBarController
.
Note: You may be wondering why we didn’t choose a UITabBarController subclass instead. As you can see that option wasn’t even offered. This is because UITabBarController is not meant to be subclassed. Instead, we create a generic view controller to which a tab bar controller will be assigned.
2. Create DvdStatsViewController and DvdBorrowersViewController
These are the new view controllers I mentioned previously. We already have our
DvdInfoViewController
that will be in the first tab, so let’s create the remaining two.
In Xcode, right-click on the View Controllers
folder and choose Add -> New File
. Choose UIViewController subclass
and name the file DvdStatsViewController
.
To create
DvdBorrowersViewController
do the same thing, only name it
DvdBorrowersViewController
.
That’s it about those two controllers for now. I’m not going to cover actually filling them with any content since that’s not the point of this tutorial. We will leave them here as placeholders for future tutorials when we cover Core Data and RSS feed processing.
3. Set up DetailViewTabBarController
Since we’re going to be coming to the tab bar controller from the
RootViewController
, we need to pass it DVD info from the row that was tapped. We can do that in a custom init method in
DetailViewTabBarController
. You’ll see how it’s used later when we instantiate it in
didSelectRowAtIndexPath
method ofRootViewController
.
We’re also going to need a handle on the data about the DVD that was tapped on the front page.
Declare it all in
DetailViewTabBarController.h
@interface DetailViewTabBarController : UIViewController {
NSDictionary *dvdData;
}
-(id)initWithDvdData:(NSDictionary *)data;
@end
Next, let’s define
initWithDvdData
in
DetailViewTabBarController.m
-(id)initWithDvdData:(NSDictionary *)data {
if (self = [super init]) {
dvdData = data;
}
return self;
}
All we’re doing here is setting local instance variable
dvdData
to whatever is passed in. We now have a handle on the DVD data and can use it across all the view controllers.
4. Implement loadView method in DetailViewTabBarController
loadView
is a method on UIViewController that can be overridden in case you want to build your view from ground up. That’s exactly what we want to do so let’s override
loadView
now. Let’s discuss it piece by piece;
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
Borrowing the biblical line; in the beginning there was nothing, we need a base view to build upon. So we created an empty view with a white background that takes up the whole screen. We will attach all the other views to it.
Now we need to instantiate all three of our controllers. Each one will become one tab in the tab view.
// Declare all three view controllers
DvdInfoViewController *dvdInfoController = [[DvdInfoViewController alloc]
initWithDvdData:dvdData
nibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
DvdStatsViewController *dvdStatsViewController = [[DvdStatsViewController alloc] init];
DvdBorrowersViewController *dvdBorrowersViewController = [[DvdBorrowersViewController alloc] init];
// Set a title for each view controller. These will also be names of each tab
dvdInfoController.title = @"Info";
dvdStatsViewController.title = @"Stats";
dvdBorrowersViewController.title = @"Borrowers";
You see the
dvdInfoController
is initialized the same way we did it before in
RootViewController
. We pass it
dvdData
that we get from our custom initialization method.
Now the meat of this method, let’s create the actual tab bar controller and give it the three view controllers. You can note that we’re setting the bounding frame to be 320 pixels wide and 460 pixels high. We shave off 20 pixels because the title bar is already taking up, well, 20 pixels.
UITabBarController creates tabs when you call setViewControllers on it and pass it an array with your controllers in it. That’s what’s happening here.
// Create an empty tab controller and set it to fill the screen minus the top title bar
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
// Set each tab to show an appropriate view controller
[tabBarController setViewControllers:
[NSArray arrayWithObjects:dvdInfoController, dvdStatsViewController, dvdBorrowersViewController, nil]];
And finally, let’s clean up objects we no longer need from memory and add the tab controller view to our parent view so we can actually see it.
// Clean up objects we don't need anymore
[dvdInfoController release];
[dvdStatsViewController release];
[dvdBorrowersViewController release];
// Finally, add the tab controller view to the parent view
[self.view addSubview:tabBarController.view];
}
5. Modify RootViewController’s didSelectRowAtIndexPath method to display DetailViewTabBarController
The last step we need to take is to modify
didSelectRowAtIndexPath
in
RootViewController
to display our newly created tab bar controller instead of theDvdInfoViewController
we showed in the last tutorial.
Open up
RootViewController.m
and modify
didSelectRowAtIndexPath
to look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewTabBarController *controller = [[DetailViewTabBarController alloc]
initWithDvdData:[dao libraryItemAtIndex:indexPath.row]];
controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
At this point, you can run your project and tapping on a DVD title should take you to the tab bar controller which has three tabs. The first tab contains the movie info, the remaining two are empty. As I mentioned before, we’ll fill those in in the later tutorials or it can be left as an exercise for the reader.
6. Add icon to tabs
The tabs now only have a textual title. Let’s make it prettier by adding an icon to each of them. I only use one image (included in the project) for demonstration purposes but you can have a different image for each tab. The image should be 32×32 pixels PNG file. You don’t need to to create the pretty, shiny, gray and blue images. Just pass it a normal image and the SDK will do the rest.
Add this in the
loadView
method in
DetailViewTabBarViewController.m
implementation file right after we set the titles of the controllers:
// Set a title for each view controller. These will also be names of each tab
dvdInfoController.title = @"Info";
dvdStatsViewController.title = @"Stats";
dvdBorrowersViewController.title = @"Borrowers";
dvdInfoController.tabBarItem.image = [UIImage imageNamed:@"dvdicon.png"];
dvdStatsViewController.tabBarItem.image = [UIImage imageNamed:@"dvdicon.png"];
dvdBorrowersViewController.tabBarItem.image = [UIImage imageNamed:@"dvdicon.png"];
If everything went well, you should see your app looking like this
Conclusion
While it may seem easier to create everything in the Interface Builder, you can see that in this case creating a tab bar controller was a breeze. Adding new tabs/view controllers to it is as easy as adding an object to an array. I hope this tutorial connecting the dots how to go from a table view to a tab bar view in a navigation-based application.
You can download the complete source code to this tutorial here:
My DVD Library Tutorial 4
分享到:
相关推荐
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
中医诊所系统,WPF.zip
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
全国各省、297个地级市公路里程面板数据1999-2021年涵盖了中国各地区公路建设的详细情况,是衡量地区基础设施水平的重要指标。这些数据不仅包括了全国31个省份的公路里程,还深入到了297个地级市的层面,提供了从1999年至2021年的连续年份数据。这些数据来源于各省统计年鉴、经济社会发展统计数据库、地级市统计年鉴以及地级市发展统计公报,确保了数据的准确性和权威性。通过这些数据,可以观察到中国公路交通建设的发展不平衡性,沿海地区和长江中下游地区公路交通密度较高,而西部地区相对较低。这些面板数据为研究中国城市化进程、区域经济发展以及交通基础设施建设提供了宝贵的信息资源。
技术处工作事项延期完成申请单.docx
本文为图书馆管理课程设计SQL Server功能规范说明书。本说明书将: 描述数据库设计的目的; 说明数据库设计中的主要组成部分; 说明数据库设计中各功能的实现。 本文档主要内容包括对数据库设计结构的总体描述,对数据库中各种对象的描述(包括对象的名称、对象的属性、对象和其他对象直接的关系);在数据库主要对象之外,本文还将描述数据库安全性设置、数据库属性设置和数据库备份策略,为数据库管理员维护数据库安全稳定地运行提供参考;有需要的朋友可以下载看看
项目中常见的问题,记录一下解决方案
octopart数据格式样例
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
本文档主要讲述的是Oracle 11g RAC安装与配置for Linux;希望对大家的学习会有帮助 文档结构 第一部分:Oracle Grid Infrastructure安装 第二部分:Oracle Clusterware与Oracle Real Application Clusters安装前准备规程 第三部分:安装Oracle Clusterware与Oracle Real Application Clusters 第四部分:Oracle Real Application Clusters环境配置 第五部分:Oracle Clusterware与Oracle Real Application Clusters参考资料
python教程.txt
文件太大放服务器下请务必到资源详情查看后然后下载 样本图:blog.csdn.net/2403_88102872/article/details/143979016 重要说明:数据集为小目标检测,训练map精度偏低属于正常现象,只要能检测出来即可。如果map低于0.5请勿奇怪,因为小目标检测是业界公认难检测的研究方向之一。 数据集格式:Pascal VOC格式+YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):3763 标注数量(xml文件个数):3763 标注数量(txt文件个数):3763 标注类别数:7 标注类别名称:["blackheads","cyst","fore","nodule","papule","pustule","whiteheads"]
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
全国各地级市固定资产投资统计数据集覆盖了1996至2020年的时间跨度,提供了详尽的年度固定资产投资金额,单位为百万人民币。这些数据不仅包括了地级市级别的投资情况,还涵盖了省、区县以及行业等多个维度,为研究区域经济增长、投资结构和发展趋势提供了宝贵的数据支持。固定资产投资作为衡量一个地区经济发展活力和潜力的重要指标,反映了社会固定资产在生产、投资额的规模和速度。通过这些数据,研究人员可以深入分析不同地区、不同行业的投资特点,以及随时间变化的趋势,进而为政策制定和经济预测提供科学依据。
training_plan_db.sql
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
5
全国各省地区城乡收入差距、泰尔指数、城镇农村居民可支配收入统计数据集提供了1990至2021年间的详细数据,覆盖全国31个省份。该数据集不仅包括城镇居民和农村居民的人均可支配收入,还涵盖了乡村人口、全体居民人均可支配收入、城镇人口以及年末常住人口等关键指标。泰尔指数作为衡量收入不平等的重要工具,通过计算城镇收入与农村收入之比,为研究者提供了一个量化城乡收入差距的科学方法。这些数据不仅有助于分析中国城乡之间的经济差异,还能为政策制定者提供决策支持,以缩小城乡差距、促进区域均衡发展。数据集的丰富性使其成为社会科学领域研究城乡发展、收入分配不平等等问题的宝贵资源。
FileName.zip
java面向对象 - 类与对象java面向对象 - 类与对象代码.zip