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];
|
}
|
分享到:
相关推荐
在iOS应用开发中,`UITabBar`是苹果提供的一个标准组件,用于在底部展示多个选项卡,用户可以通过点击选项卡在不同的视图之间切换。然而,标准的`UITabBar`是固定的,通常只能容纳有限数量的标签,并且它们默认是...
iphone中的基础控件UITabBar及UITabBarController,适合初学者,主要学习UITabBarController的常用属性设置及其点击事件。 1、UITabBarController是一个容器,一般作为整个程序的rootViewController,容器包含多个...
在iOS应用开发中,`UITabBar` 是一个标准组件,用于展示多个主要功能选项,用户可以通过点击不同的标签在各个页面之间切换。然而,系统默认的`UITabBar`样式可能无法满足所有设计需求,因此开发者经常需要自定义`...
1.自定义中间带弧度的UITabBar(参照代码思路可改成其它形状)。 2.TabBar保留系统原有push和pop过渡效果。 3.由于自定义了UITabBar所以就连同TabBarController也一同定义了(用法和系统的很类似)。
在iOS应用开发中,`UITabBar` 和 `UINavigationController` 是两个非常重要的组件,它们在构建用户界面时起着核心作用。`UITabBar` 用于实现底部的标签栏,提供多页面间的切换,而 `UINavigationController` 则负责...
`UITabBar`作为底部导航栏,是用户交互的重要部分。有时,开发者需要对其进行自定义,以满足特定的设计需求,比如在中间添加一个突出的按钮。本教程将详细讲解如何在Objective-C(OC)中实现一个自定义的`UITabBar`...
在iOS应用开发中,`UITabBar`是苹果提供的一个标准组件,用于在底部展示多个选项卡,每个选项卡代表一个不同的视图控制器。然而,系统默认的`UITabBar`样式可能无法满足所有设计需求,因此开发者经常需要对其进行...
在实际应用中,我们有时需要在用户切换`UITabBarItem`时动态改变对应的`UINavigationController`的根视图控制器。本教程将详细讲解如何通过`Notification`来实现在`UITabBar`切换时变换`TabBar`下的`...
在iOS应用开发中,`UITabBar` 是一个非常重要的组件,它用于在底部展示多个选项卡,方便用户在不同的视图控制器之间进行切换。在本项目“ios-UITabBar 点击动画效果 Q弹.zip”中,开发者实现了一个独特的`UITabBar...
在iOS开发中,`UITabBar`是苹果提供的一个核心组件,用于构建具有底部标签导航的应用界面。在Xcode 7.2.1版本中,我们可以利用它来为6s Plus或其他兼容iOS设备创建美观且功能丰富的用户界面。`UITabBar`与`...
iOS5以后对UITabBar提供了很多新的方法和属性,就不用像以前一样为实现自定义的样式而大费周折了。基于系统的UITabBar实现主要是为了实现稳定性。而且用第三方的话,学到的知识价值没有用系统的高。
UITabBar 点击当前选中的 item 时触发下拉刷新。 使用 runtime hook UITabBar 的点击事件,不依赖 UITabBarControllerDelegate,支持刷新动画。 https://github.com/xiaopin/UITabBarRefresh.git
在iOS应用开发中,`UITabBar`是苹果提供的一个标准组件,用于在底部展示多个主功能间的切换。然而,为了使应用具有独特的设计风格或更丰富的交互效果,开发者经常会选择自定义`UITabBar`。标题“简单的自定义...
自定义UITabBar,layoutSubviews重写UITabBarButton位置,重写则hitTest方法并监听按钮的点击 自定义的UITabBarController和UINavigationController
UITabBar是iOS开发中的一个重要组件,它是底部导航栏,通常包含多个标签,用户可以通过点击这些标签在不同的视图控制器间切换。在本教程中,我们将深入探讨`uitabbar`的高级应用,特别是如何利用它来实现各种自定义...
在iOS应用开发中,`UITabBar` 是一个标准组件,用于展示多个可选视图控制器,用户可以通过点击底部的图标在这些视图之间切换。然而,为了满足个性化设计需求,有时我们需要对`UITabBar`进行自定义,比如让中间的按钮...
在iOS开发中,`UITabBar`是苹果提供的一个标准组件,用于展示应用程序的主要功能选项。用户通过点击底部的图标可以在各个子视图控制器之间切换。然而,有时开发者需要根据应用设计的需求对`UITabBar`进行自定义,以...
`UITabBar` 是这个组件的重要组成部分,通常显示在屏幕的底部,用于展示各个选项卡。本教程将详细介绍如何在iOS应用中隐藏和显示`UITabBar`,并提供一个相关的代码示例——"隐藏底部UITabbarDemo"。 首先,我们需要...
在iOS应用开发中,`UITabBar`是系统提供的一个底部导航组件,它通常用于展示应用的主要功能模块,用户可以通过点击不同的tab切换不同的界面。然而,系统默认的`UITabBar`样式可能无法满足所有设计需求,这时就需要...
iphone开发基础UITabBar和UINavigation搭建简单应用,适合初学者,学习tabbar和navigation,这里练习两者组合搭建一个简单的常见应用例子, 其实普通应用也就是tabbar作为根视图,每个tab又是一个navigation的根...