How to add a custom view to a XIB file defined view in monotouch

南楼画角 提交于 2019-11-27 02:20:19

问题


I'm trying to learn monotouch at the moment, and following the learn monotouch book by Mike BlueStein. Not a bad book, but it's slightly outdated since xcode 4 (i believe) and a newer version on monotouch has come out.

Anyways, in my project I have a controller and a xib file. I also have a custom view (e.g. myview : UIView), that overrides the draw method. I want to show my custom view next to or on top of the view defined in the xib file. How do I do this?

In the controller, If I override the LoadView method, and set the View to an instance of my custom view, then I can see it, but I loose everything defined in the xib file. If I try to add as a sub view, it does not appear at all.

What am I missing? If the question is not clear, please ask me, so I can clarify.

Cheers.


回答1:


Follow the following steps to use a custom view in a XIB:

First, decorate the view with the RegisterAttribute:

[Register("MyView")]
public class MyView : UIView
{
}

and implement the following constructor:

public MyView(IntPtr handle) : base(handle) {}

This constructor is needed for when the runtime will try to recreate the view after it has been destroyed by a memory warning. After you have created your custom class:

  • Open the XIB in Xcode (always by double-clicking on it through MonoDevelop) and add a UIView where you want it.
  • In Xcode, set that UIView's class to MyView (or whichever name you passed to the RegisterAttribute):

  • Compile-Run.

EDIT:

Do not override LoadView for controllers that are loaded from a XIB. LoadView is meant to create the controller's view when that controller's view is not loaded from a XIB.




回答2:


Thanks Dimitris, great answer.

For those who get confused like me, here is the simplist procedure to add and use a Xib file as a partial / subview:

  1. Add a new Xib File in MonoDevelop (ie LoginView.xib)
  2. Add a new (partial) Class, this will be the custom class (The Code-behind file let 's say) for the view. Give it any name "LoginView.cs")
  3. Add the Attribute (RegisterAttribte) and the Constrctor as exaplain above by Dimitris.
    1. Double click the LoginView.xib to open it in XCode IB. Then Change the Custom Class Attribute of the xib file to point to your "code-behind file" (ie LoginView.cs)
  4. Add any outlets or action if you need. MonoDevelop will generate a .designer file and attach it to your code-behind file, this is where you can see all your outlets and actions.
  5. In your controllers where you want to add this view, load your .xib file as follows:

    var views = NSBundle.MainBundle.LoadNib("LoginView", this, null); LoginView loginView = Runtime.GetNSObject(views.ValueAt(0)) as LoginView; mainLayout.AddSubview(loginView); // where mainLoyout is the placeHolder in my main ViewController

These three lines are based on flexaddicted answer here



来源:https://stackoverflow.com/questions/9857558/how-to-add-a-custom-view-to-a-xib-file-defined-view-in-monotouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!