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

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
主站蜘蛛池模板: 于都县| 双城市| 南昌县| 鱼台县| 嘉义县| 宁蒗| 温宿县| 南溪县| 祥云县| 通榆县| 兴和县| 明光市| 长泰县| 绍兴市| 常山县| 离岛区| 高唐县| 花莲县| 宜丰县| 阿勒泰市| 罗城| 米脂县| 蓝山县| 金乡县| 富顺县| 卢湾区| 武威市| 临武县| 荥阳市| 图木舒克市| 唐河县| 开阳县| 旺苍县| 思茅市| 秀山| 棋牌| 大竹县| 拜城县| 兴安县| 惠来县| 青川县|