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

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
主站蜘蛛池模板: 招远市| 耿马| 巫溪县| 福州市| 鸡西市| 阿勒泰市| 铜川市| 莱阳市| 荔波县| 和硕县| 衡山县| 祁门县| 武冈市| 翼城县| 新乐市| 宕昌县| 米易县| 上蔡县| 南汇区| 绥芬河市| 柯坪县| 莱芜市| 丹东市| 潞西市| 漳浦县| 育儿| 冕宁县| 镶黄旗| 巫溪县| 岗巴县| 鄂托克旗| 金华市| 雷山县| 肥乡县| 新源县| 察雅县| 武清区| 清水河县| 安阳市| 江源县| 东安县|