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

Loading a view with a view controller

In this recipe, we will learn how to use the UIViewController class to manage views.

Getting ready

Create a new iPhone Empty Project in Xamarin Studio and name it ViewControllerApp.

How to do it...

Perform the following steps to load a view with a view controller:

  1. Add a new file to the project.
  2. Right-click on the project in the Solution pad and go to Add | New File….
  3. In the dialog that will appear, select iPhone View Controller from the iOS section. Name it MainViewController and click on the New button. Xamarin Studio will create a new XIB file and will automatically open the MainViewController.cs source file. This file contains a class that overrides the UIViewController class, and we can implement any code related to our view controller in it.
  4. Open the MainViewController.xib file in Interface Builder.
  5. Add UILabel on the view.
  6. Create and connect an outlet for it inside the MainViewController class and name it myLabel.
  7. Enter the text View in controller! in the label.
  8. Save the XIB document.
  9. In Xamarin Studio, enter the following code in the FinishedLaunching method of the AppDelegate class, right after the window initialization line:
    MainViewController mainController = new MainViewController ();
    window.RootViewController = mainController;
  10. Compile and run the app on the simulator.

How it works...

When we add a new iPhone View Controller file in a project, in this case MainViewController, Xamarin Studio basically creates and adds the following three files:

  • MainViewController.xib: This is the XIB file that contains the controller.
  • MainViewController.cs: This is the C# source file that implements the class of our controller.
  • MainViewController.designer.cs: This is the autogenerated source file that reflects the changes we make to the controller in Interface Builder.

Notice that we do not need to add an outlet for the view as this is taken care of by Xamarin Studio. We initialize the controller through its class, as follows:

MainViewController mainController = new MainViewController ();

Then, we assign the controller to the window.RootViewController property, as follows:

window.RootViewController = mainController.

Our view controller is now the root view controller of our app's window, and it is the first one that will be shown when the app starts.

There's more...

The project we have just created only shows how we can add a controller with a view. Notice that we created the outlet for the label inside the MainViewController class, which acts as the file's owner object in the XIB file. To provide some functionality for the MainViewController class, add the following method in the MainViewController class in the MainViewController.cs file:

public override void ViewDidLoad ()
{
  this.myLabel.Text = "View loaded!";
}

This method overrides the UIViewController.ViewDidLoad() method, which is executed after the controller has loaded its view.

UIViewController methods to override

The UIViewController class contains a number of methods that allow us to manage the view controller's life cycle. These methods are called by the system on the view controller, and we can override them to add our own implementation. Some of these methods are as follows:

  • ViewWillAppear: This method is called when the controller's view is about to appear.
  • ViewDidAppear: This method is called when the controller's view has been displayed.
  • ViewWillDisappear: This method is called when the controller's view is about to disappear, for example, when another controller will be displayed.
  • ViewDidDisappear: This method is called when the view has disappeared.

See also

  • The Navigating through different view controllers recipe
  • The Creating an iOS project with Xamarin Studio and Accessing the UI with Outlets recipes from Chapter 1, Development Tools
主站蜘蛛池模板: 乾安县| 华蓥市| 景泰县| 遂宁市| 南城县| 瑞丽市| 江达县| 巴东县| 宁海县| 阳原县| 东兰县| 乐至县| 璧山县| 永丰县| 千阳县| 沁源县| 剑河县| 西乌珠穆沁旗| 法库县| 苍山县| 凤翔县| 时尚| 红安县| 兴城市| 辽阳县| 和林格尔县| 浏阳市| 沅江市| 康乐县| 石台县| 盐城市| 潍坊市| 广德县| 塔河县| 曲阜市| 冷水江市| 固安县| 建昌县| 博乐市| 银川市| 佛坪县|