官术网_书友最值得收藏!

Providing controllers in tabs

In this recipe, we will learn how to display multiple view controllers in a tabbed interface.

Getting ready

The UITabBarController class provides a way to display different view controllers on the same hierarchy level divided into a tab-like interface. Create a new iPhone Empty Project in Xamarin Studio and name it TabControllerApp.

How to do it…

Perform the following steps to provide controllers in tabs:

  1. Add two iPhone view controllers to the project. Name them MainController and SettingsController.
  2. Add the following code to the ViewDidLoad method of MainController:
    this.View.BackgroundColor = UIColor.Blue;
  3. Add the following code to the ViewDidLoad method of SettingsController:
    this.View.BackgroundColor = UIColor.Yellow;
  4. Add the following code to the FinishedLaunching method of the AppDelegate class:
    MainController mainController = new MainController();
    SettingsController settingsController = new SettingsController();
    UITabBarController tabController = new UITabBarController();
    tabController.SetViewControllers(new UIViewController[] {
      mainController,
      settingsController
    }, true);
    tabController.TabBar.Items[0].Title = "Main";
    tabController.TabBar.Items[1].Title = "Settings";
    window.RootViewController = tabController;
  5. Run the app on the simulator. Click on each of the tabs at the bottom. The interface should be similar to the following screenshot when MainController is selected:
    How to do it…

How it works...

The UITabBarController class displays one tab for each of the controllers it manages. That tab is of the UITabBarItem type that can accept both text and images. We set the controllers it will display through its SetViewControllers property, as follows:

tabController.SetViewControllers(new UIViewController[] {
  mainController,
  settingsController
}, true);

After we have added the controllers, we can access its tab bar items through the TabBar property. In this case, we set the tab's Title attribute:

tabController.TabBar.Items[0].Title = "Main";

Each UIViewController contains a TabController property. Similar to the NavigationController property, when the controller is part of a tab controller, the property will return the instance of that tab controller.

There's more...

The controller can accept as many controllers as we want but if we add six or more, four will be displayed with their tabs, while a fifth predefined More tab will represent all the remaining controllers. This is to keep the interface easily accessible to the user by keeping the tabs to a specific size suitable for human fingers. When we add more than six controllers in a tab bar controller interface, by default, the object provides an Edit button on top in the More tab that allows the user to rearrange the order of controllers. If we want to exclude some controllers from this functionality, we have to remove it from the CustomizableViewControllers array.

Useful UITabBarController properties

Some more useful properties of the UITabBarController class are as follows:

  • ViewControllers: This is an array containing all the controllers that the tab controller holds.
  • SelectedIndex: This is the zero-based index of the selected tab. Setting this property to the desired index programmatically selects the corresponding controller.
  • SelectedViewController: This is the currently selected controller.

Determining tab selection

To determine when the user has selected a tab on a tab controller, we can subscribe to its ViewControllerSelected event:

tabController.ViewControllerSelected += (sender, e) => {
  // Do something with e.ViewController.
};

See also

  • The Using view controllers efficiently recipe
主站蜘蛛池模板: 揭西县| 乌兰县| 屏东市| 民丰县| 延寿县| 清河县| 阿拉尔市| 疏勒县| 宁陕县| 柞水县| 荥阳市| 五大连池市| 兴化市| 桃江县| 通州市| 祁连县| 濮阳市| 长子县| 邻水| 龙游县| 古浪县| 遂宁市| 疏勒县| 金坛市| 湖北省| 双柏县| 铁力市| 苍溪县| 淮安市| 莱西市| 衡阳县| 浦北县| 岳普湖县| 梅河口市| 当雄县| 彰化市| 景东| 沂南县| 大渡口区| 隆林| 新沂市|