开发者

Bind a custom control created as UIView in Interface Builder via monotouch

Using monotouch and monodevelop, I'd like to create a custom control. I followed this steps:

  • add a new file as "iPhone View" (calling it TestView)
  • edit TestView.xib in Interface Builder, es. changing it's background and size
  • edit MainWindow.xib in Interface Builder, adding a UIView and setting it's class identity as "TestView".

At this point, I'd like to launch the application and see in the UIView of MainWindow the content of an instance of TestView.

In order to get this "binding" I tried several steps (I know it can be done via code creating the outlets, etc.., but I'd like to understand if it can be done via Interface builder).

In one of the methods tried, I set via Interface Builder the value "TestView" as class identifier in the View of TestView.xib: in this way MonoTouch created the code in TestView.xib.designer.cs.

[MonoTouch.Foundation.Register("TestView7")]
public partial class TestView7 {
}

Then, in TestView.xib.cs, I added:

public partial class TestView : UIView
{
    public TestView (IntPtr p) : base(p)
    {
    }       
 }

When I launch the app, I cannot see in the view of MainWindow the content of TestView, but if in TestView constructor I add something such as

BackgroundColor = UIColor.Yellow;

then, I can see TestView in MainWindow... or better I can see only the yellow rectangle, not the real co开发者_如何学JAVAntent!

So, the problem is that TestView is binded to the UIView in MainWindow, but it's content comes only from the code and not from the content defined via Interface Builder in TestView.xib.

Is there a way to load the content from the TestView.xib?


Take a look at http://ios.xamarin.com/Documentation/API_Design#Interface_Builder_Outlets_and_C.23

I believe what you need to do is add a constructor that handles the nib file (The following is from the above link):

When using MonoTouch your application will need to create a class that derives from UIViewController and implement it like this:

public class MyViewController : UIViewController {
    public MyViewController (string nibName, NSBundle bundle) : base (nibName, bundle)
    {
    // You can have as many arguments as you want, but you need to call
    // the base constructor with the provided nibName and bundle.
    }
} 

Then to load your ViewController from a NIB file, you do this:

    var controller = new MyViewController ("HelloWorld", NSBundle.MainBundle, this); 

This loads the user interface from the NIB.


The answer by Allison A in SO question monotouch - reusable iOS custom view, explains how to load existing composite views created in Interface Builder into a custom view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜