How to place a monotouch custom view class in Interface Builder

In the previous entry, I addressed a calendar view class for MonoTouch. Now I try to handle the custom view class in Interface Builder. The procedure is the same as addressed in a article MonoTouch Custom ViewController, Adding Stepwise Subviews – Knowing .NET, and is the following:


First adding a Register attribute to custom view class. Different from the previous entry, that class must have a constructor witch has a argument type of IntPtr. This is because runtime calls the constructor to instance it.


And at last, creating a new nib file in MonoDevelop and opening it in InterfaceBuilder. In InterfaceBuilder, D&Drop a UIView and changing its class identity to the custom view class name (In this case "CalendarView").

前回のエントリでMonoTouchのカレンダービューを作りました.今回はそれをInterfaceBuilderで扱う手順を紹介します.手順自体はとても単純で,MonoTouch Custom ViewController, Adding Stepwise Subviews – Knowing .NET で述べられている方法と同じです.上の画像を使い説明していきます.
まずクラスにRegister属性をつけます.このRegister属性の引数文字列にInterfaceBuilderで参照したいクラス名を指定します.任意の名前がつけられますがクラス名と合わせておくのが無難でしょう.またInterfaceBuilderは,この例では net.ReinforceLab....CalendarViewのような,クラスの完全名を受け付けません (入力してもキャンセルされます,ピリオドがだめなでしょうか?).したがってここではクラス名だけを指定しています.
次にIntPtr型の引数をとるコンストラクタを追加します.これはNIBファイルを元にランタイムがこのクラスのインスタンスを作成するときにはIntPtrを引数にコンストラクタを呼び出すためです.
これでInterfaceBuilderでカスタムViewクラスを扱う準備ができました.MonoDevelopで適当なXIBファイルを作り,そこにUIViewのインスタンスを置きます.次にIdentifyウィンドウでクラス名に先ほどRegister属性で指定した名前を設定します.背景色やサイズなども合わせて設定しておきます.
最後にMonoDevelopでビルド,実行するだけです.

MonoDevelopは,Export属性でObjective-Cセレクタとの結合を,Register属性でクラス名との結合を提供すると覚えておけば応用も利くかもしれません.