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

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
主站蜘蛛池模板: 宝清县| 洞头县| 阿尔山市| 博客| 蕉岭县| 东丰县| 马龙县| 武邑县| 昌邑市| 古丈县| 中西区| 福州市| 城口县| 炉霍县| 湟源县| 崇信县| 汾阳市| 哈尔滨市| 五大连池市| 乌苏市| 紫云| 青海省| 拉萨市| 舟曲县| 安西县| 宜兰市| 武安市| 奎屯市| 古蔺县| 漳平市| 农安县| 台湾省| 渝北区| 石河子市| 永川市| 临汾市| 贵德县| 德钦县| 通化市| 南部县| 满洲里市|