有关于如何使用 UITabBarController 的简单易懂的示例吗?
我想使用 UITabBarController 但发现这非常困难。我有一本书在一个巨大的项目中使用它,我必须花一周左右的时间完成该项目,然后找出哪些部分属于使 UITabBarController 工作。也许你知道一篇关于这方面的好文章?
I want to use a UITabBarController but find that very difficult. I have a book that uses it in a giant project where I would have to complete that for a week or so and then find out what parts belong to making the UITabBarController work. Maybe you know of an good article about that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我强烈建议您阅读视图控制器编程指南的 标签栏控制器部分。它充满了简单的示例和清晰的解释。
I highly recommend going though the View Controller Programming Guide's Tab Bar Controller section. It's full of bare-bones examples and clear explanations.
这里'完整的示例,这是简短的版本:
您可以从“基于导航的应用程序”开始,基本上将导航控制器替换为选项卡栏控制器。
通常,您将向 MainWindow.xib 添加一个选项卡栏控制器对象(只需将其从库中拖出并拖入您的视图中 - 删除导航控制器后),并将其挂接到应用程序委托中的出口。在您的
applicationDidFinishLaunching:
中,在[window makeKeyAndVisible];
: 之前添加以下行:(这可能会替换与默认情况下非常相似的内容)。
现在您需要为每个选项卡定义视图控制器。创建类文件后,进入 MainWindow.xib 并将项目添加到选项卡栏。对于每个选项卡,将对象类型(检查器上的第四个选项卡)更改为您为该特定选项卡的视图控制器选择的类名称。
简而言之就是这样。
Here's a full example, and here's the short version:
You can start with a "Navigation-Based Application" and basically swap out the navigation controller for a tab bar controller.
Typically, you'll add a Tab Bar Controller object to your MainWindow.xib (just drag it out of the library and into your view -- after deleting the navigation controller), and hook it to an outlet in your application delegate. In your
applicationDidFinishLaunching:
add the following line before[window makeKeyAndVisible];
:(this will probably replace something very similar to what is there by default).
Now you'll want to define the view controllers for each tab. Once you have class files created, go into your MainWindow.xib and add items to your tab bar. For each, change the object type (fourth tab on the inspector) to the class name you've chosen for that particular tab's view controller.
That's it in a nutshell.
一旦您了解了其组织,标签栏实际上很容易理解。
您有一个选项卡栏控制器,可以在其中放置任意数量的视图控制器。当按下选项卡时,该选项卡的视图控制器将被激活并可见。
理解起来比较棘手的一点是,NavigationController 也是 ViewController。因此,如果您想要对任何一个选项卡进行导航,则必须添加一个导航控制器来保存用于显示的视图控制器。
尽管您可以在 IB 中制作选项卡栏控制器,但我认为从头开始对其进行编程非常有意义,因为您可以更好地理解它们的布局方式。
A tab bar is actually pretty easy to understand, once you know the organization.
You have a tab bar controller, into which you can put any number of view controllers. As the tabs are pressed the view controller for that tab will be made active and visible.
The tricky thing to understand is, that NavigationControllers are also ViewControllers. So if you want navigation for any one tab, you have to add a navigation controller which holds the view controller you use for display.
Although you can do tab bar controllers in IB, this is the one area where I think starting by programming them from scratch makes a ton of sense, because you better understand how they are laid out.
斯坦福大学讲座(由 Apple 工程师主持)在第 7 讲中对此进行了介绍:http ://www.stanford.edu/class/cs193p/cgi-bin/index.php
向下滚动到第 7 讲,其中涵盖导航控制器、应用程序数据流、自定义导航和选项卡栏控制器,还涵盖组合方法(即 UITabBarControllers 和 UINavigationControllers 在一起)。有 PDF,如果您想观看讲座视频(推荐),那么您可以在 iTunes U 上找到它的链接。
Standford University lectures (run by Apple Engineers) cover this in Lecture 7: http://www.stanford.edu/class/cs193p/cgi-bin/index.php
Scroll down to Lecture 7 which covers Navitgation Controller, Application Data Flow, Customizing Navigation and Tab Bar Controller, and also covering Combining Approaches (i.e. UITabBarControllers and UINavigationControllers together). There's the PDF and if you want to watch the video of the lecture (recommended) then you can find the link to it on iTunes U.