- iOS Development with Xamarin Cookbook
- Dimitris Tavlikos
- 580字
- 2021-07-16 11:45:10
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
- 造個(gè)小程序:與微信一起干件正經(jīng)事兒
- 計(jì)算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- 深入理解Django:框架內(nèi)幕與實(shí)現(xiàn)原理
- Web Application Development with R Using Shiny(Second Edition)
- 青少年信息學(xué)競(jìng)賽
- Visual Basic程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)(第二版)
- C# Multithreaded and Parallel Programming
- 移動(dòng)增值應(yīng)用開發(fā)技術(shù)導(dǎo)論
- Learning iOS Security
- 寫給青少年的人工智能(Python版·微課視頻版)
- Python無(wú)監(jiān)督學(xué)習(xí)
- Parallel Programming with Python
- 你必須知道的.NET(第2版)
- CryENGINE Game Programming with C++,C#,and Lua
- Mastering JavaScript Promises