Creating the Objects in Interface Builder
The process for combining tab bar and navigation controllers in a nib file is relatively straightforward. The only real difference is how you create the relationship between the tab bar controller and the navigation controller. When using these objects by themselves, each one takes on the role of the root view for the application’s window. When combined, however, only the tab bar controller assumes this role. Instead of providing the root view for the window, the navigation controller acts as the root view for a tab in the tab bar interface.
Figure 7-1 shows the configuration of the objects that you need to create in your nib file. In this example, the first three tabs of the tab bar interface use custom view controllers but the last tab uses a navigation controller. One additional view controller is then added to act as the navigation controller’s root view controller. To manage memory better, each of the custom view controllers (including the root view controller of the navigation controller) stores its corresponding view in a different nib file.
Figure 7-1 Mixing navigation and tab bar controllers in a nib fileAssuming you are starting from a generic main nib file (one that does not include a tab bar controller), you would use the following steps to create the objects from Figure 7-1 in Interface Builder:
-
Drag a tab bar controller object from the library to your Interface Builder document window.
When you add a tab bar controller to a nib file, Interface Builder also adds a tab bar view, two root view controllers, and two tab bar items (one for each view controller).
-
Save a reference to the tab bar controller using an outlet.
In order to access the tab bar controller at runtime, you either need to use an outlet or you must explicitly retrieve the nib file’s top-level objects when you load the nib file. Using an outlet is generally much simpler. To add an outlet for both the tab bar controller and window, you need to include code similar to the following in your application delegate’s header file:
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
|
UITabBarController* tabBarController;
|
UIWindow *window;
|
}
|
@property (nonatomic, retain) IBOutlet UIWindow *window;
|
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
|
@end
|
After adding the outlet definition, create a connection from that outlet to the tab bar controller object.
-
Synthesize the properties from the preceding step by adding the following code to the implementation file of your application delegate class:
@synthesize window;
|
@synthesize tabBarController;
|
-
Add one View Controller object and one Navigation Controller object to the tab bar controller.
The number of view controllers embedded inside your tab bar controller object determines the number of tabs displayed by your tab bar interface. Because the initial tab bar controller already has two generic view controllers, you need to add one more View Controller object (UIViewController
) and one Navigation Controller object (UINavigationController
).
To add a view controller, do one of the following:
-
Drag the appropriate object from the library to the tab bar in the edit surface.
-
Drag the object from the library to the tab bar controller in your Interface Builder document window. The window must be in outline mode.
When adding the navigation controller, you should either drag the appropriate object from the library or select the tab bar controller object and configure your view controller types using the Attributes inspector. Both of these options add the correct type of view controller object to your nib file. You should never add a navigation controller by dragging a generic View Controller object to the nib file and change its class name to the desired class type.
To delete a view controller, select the view controller object in the edit surface or document window and press the Delete key.
-
Arrange the view controllers in the order you want them to appear in the tab bar interface.
You can rearrange view controllers (and the corresponding tabs) by dragging the tabs displayed on the tab bar controller edit surface or by dragging the view controllers in the Interface Builder document window (outline mode only). Although the edit surface shows all of the tabs, only five are displayed at runtime. If your tab bar controller contains six or more view controllers, only the first four are displayed in the tab bar initially. The last spot on the tab bar is reserved for the More view controller, which presents the remaining view controllers.
-
Configure the view controllers.
For each root view controller, you should configure the following attributes:
-
Set the class of each custom view controller object using the Identity inspector. For generic View Controller objects, change the class name to the custom subclass you want to use to display the contents of that tab. Do not change the class of the Navigation Controller object itself but do set the class of the navigation controller’s embedded custom view controller.
-
Provide a view for each custom view controller. The preferred way to do this is to configure the NIB Name attribute of each custom view controller with the name of the nib file containing the view. Although you can include each view in the same nib file as the view controller, doing so is not recommended. For information on how to configure the nib file for a custom view controller, see “Storing the View in a Detached Nib File.”
-
As appropriate, configure any style or appearance information for any of the view controllers.
-
Configure the tab bar item for each view controller.
You can select the tab bar item from the tab bar controller edit surface or from the Interface Builder document window when it is in outline or browser modes. Using Interface Builder, you can specify the title, image, and badge of a tab bar item. Alternatively, you can set the tab bar item to one of the standard system tabs by assigning a value to the Identifier property in the Attributes inspector.
-
Save your nib file.
Although the preceding steps configure the tab bar interface, they do not install it in your application’s main window. To do that, you need to add some code to the applicationDidFinishLaunching:
method of your application delegate, as shown in Listing 7-1. This is where you use the outlet you created for the tab bar controller in your application delegate.
Listing 7-1 Installing the combined interface in your application’s window
- (void)applicationDidFinishLaunching:(UIApplication *)application {
|
[window addSubview:tabBarController.view];
|
}
|
分享到:
相关推荐
iphone中的基础控件UITabBar及UITabBarController,适合初学者,主要学习UITabBarController的常用属性设置及其点击事件。 1、UITabBarController是一个容器,一般作为整个程序的rootViewController,容器包含多个...
iphone 水平滚动的UITabBar Demo iphone 水平滚动的UITabBar Demo
1.自定义中间带弧度的UITabBar(参照代码思路可改成其它形状)。 2.TabBar保留系统原有push和pop过渡效果。 3.由于自定义了UITabBar所以就连同TabBarController也一同定义了(用法和系统的很类似)。
UITabBar和UINavigation组合使用
自定义UITabbar
自己定义的UITabBar有动画效果动画效果gif图片看博客
iOS5以后对UITabBar提供了很多新的方法和属性,就不用像以前一样为实现自定义的样式而大费周折了。基于系统的UITabBar实现主要是为了实现稳定性。而且用第三方的话,学到的知识价值没有用系统的高。
UITabBar 点击当前选中的 item 时触发下拉刷新。 使用 runtime hook UITabBar 的点击事件,不依赖 UITabBarControllerDelegate,支持刷新动画。 https://github.com/xiaopin/UITabBarRefresh.git
UITabBar 点击动画效果,点击UITabBar页面切换的同时,被点击的UITabBar会有一个动画效果,类似于果冻效果
iphone开发基础UITabBar和UINavigation搭建简单应用,适合初学者,学习tabbar和navigation,这里练习两者组合搭建一个简单的常见应用例子, 其实普通应用也就是tabbar作为根视图,每个tab又是一个navigation的根...
在tabbar进行切换的时候,变化tabbar下面的UINavigationController。通过Notification,进行通知处理
uitabbar的高级应用,分了几种不同的模版
简单的自定义UITabBar,很简单,大家自己可以自己拓展
正中间的按钮突出显示,为半圆形的UITabBar
UITabbar 基本的隐藏底部选项卡栏显示demo
iOS 自定义UITabBar中间按钮(OC版), 创建一个自定义的YLTabBar继承自UITabBar
从设备中读取图片、视频和音乐,可进行浏览、播放的应用程序,用UITabbar切换,并用UICollectionView展示,UICollectionView每个Cell显示图片、视频的缩略图(音乐的封面图片),并显示其文件名,点击每一个Cell在本...
UITabBarItem-CustomBadge, UITabBarItem CustomBadge是改变 IOS UIBadgeView方面的一种变通方法 uitabbaritem customBadgeUITabBarItem CustomBadge是改变 IOS UIBadgeView方面的一种变通方法许可证:麻省理工学院...
ios xcode 7.2.1版本6splus 运行
UITabbar替代品 SYTabBar ,SYTabBar,可替代UITabbar,可以实现tabbar的大部分功能,并支持三种style:纯...