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

Creating a custom view controller

In this recipe, we will learn how to create a subclass of UIViewController and use it to derive view controllers that were created in Interface Builder.

Getting ready

In this recipe, we will create a custom view controller that will act as a base controller, providing common functionality among its inheritors. Create a new iPhone Empty Project in Xamarin Studio and name it CustomControllerApp.

How to do it...

Perform the following steps:

  1. Right-click on the project in the Solution pad and go to Add | New File….
  2. In the dialog that appears, navigate to General | Empty Class. Name the file BaseController and click on the New button.
  3. Open the BaseController.cs file that was just created and modify it to match the following code:
    using System;
    using MonoTouch.UIKit;
    using MonoTouch.Foundation;
    using System.Drawing;
    
    namespace CustomControllerApp {
    public class BaseController : UIViewController {
    
      //Constructor
      public BaseController (string nibName, NSBundle bundle) : base(nibName, bundle) {}
    
      public override void TouchesMoved (NSSet touches, UIEventevt)
      {
        base.TouchesMoved (touches, evt);
        // Capture the position of touches
        UITouch touch = touches.AnyObject as UITouch;
        if (null != touch) {
          PointF locationInView = touch.LocationInView (this.View);
          Console.WriteLine ("Touch coordinates: {0}", locationInView);
      }
    }
  4. Now, add an iPhone view controller to the project and name it DerivedController. Change the class it inherits from UIViewController to BaseController in its class definition: public partial class DerivedController : BaseController.
  5. Set the derived controller to be the root view controller of the main window (in AppDelegate.cs):
    DerivedController derivedController = new DerivedController();
    window.RootViewController = derivedController;
  6. Compile and run the app on the simulator. Click-and-drag the mouse pointer on the white surface and watch Xamarin Studio's application output pad displaying the current position of the pointer on the simulator's screen.

How it works...

What we have done here is that we have created a base controller class that can be used in multiple Xamarin.iOS projects. The functionality we have added to this controller is to respond to user touches. Any controller that inherits it will inherit the same functionality. The code we have added to create the BaseController class is fairly simple. To make this work, we have added the following constructor to the class:

public BaseController (string nibName, NSBundle bundle) : base(nibName, bundle) {}

This is the base constructor that will get called when we initialize the DerivedController class with the new keyword, this.derivedController = new DerivedController();, through our derived object's DerivedController() constructor. So, what this practically means is that we can normally use inheritance with controllers that are loaded from XIB files.

There's more...

We can also create base controllers from XIB files. However, if the XIB files contain outlets, we need to make sure to populate these outlets in our derived classes; otherwise, they will not be available in our derived controllers. For example, if we have an outlet for a button named btnStart in the base XIB file, we would have to create the following property in our derived class:

[Outlet("btnStart")]
public UIButton BtnStart {
  get { return base.btnStart; }
  set { base.btnStart = value; }
}

The Outlet attribute tells the runtime that the specific property is an outlet. Not only that, it also helps Xamarin Studio in creating the Xcode project when we are using the derived class in a XIB.

See also

  • The Loading a view with a view controller, Using view controllers efficiently, and UI flow design with storyboards recipes
  • The Adding and customizing views recipe in Chapter 2, User Interface – Views
主站蜘蛛池模板: 霸州市| 五台县| 广西| 信丰县| 枞阳县| 基隆市| 安仁县| 屏东县| 安龙县| 丽水市| 锡林郭勒盟| 纳雍县| 定日县| 固原市| 丽江市| 岳普湖县| 绥芬河市| 峡江县| 山东省| SHOW| 凯里市| 旌德县| 凤阳县| 徐水县| 江达县| 上林县| 绥芬河市| 阿克陶县| 孟津县| 于都县| 开化县| 二连浩特市| 武清区| 姚安县| 武义县| 墨江| 南皮县| 老河口市| 农安县| 淮阳县| 嘉善县|