- iOS Development with Xamarin Cookbook
- Dimitris Tavlikos
- 580字
- 2025-04-04 21:43:39
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:
- Right-click on the project in the Solution pad and go to Add | New File….
- In the dialog that appears, navigate to General | Empty Class. Name the file
BaseController
and click on the New button. - 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); } }
- Now, add an iPhone view controller to the project and name it
DerivedController
. Change the class it inherits fromUIViewController
toBaseController
in its class definition:public partial class DerivedController : BaseController
. - Set the derived controller to be the root view controller of the main window (in
AppDelegate.cs
):DerivedController derivedController = new DerivedController(); window.RootViewController = derivedController;
- 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