Alan Spark
Alan Spark

Reputation: 8302

Why isn't my custom view showing up in MonoMac?

I am using Xamarin Studio with a MonoMac project. At the moment I am trying to get a custom view to appear in my main form but it doesn't seem to be working.

These are the steps that I went through:

  1. Created new MonoMac project.
  2. Added a new Cocoa view (TestView.xib).
  3. Double clicked on TestView.xib to open in Xcode and added some components, then closed Xcode.
  4. Double clicked on MainWindow.xib to open in Xcode.
  5. Added custom view to window and set class to TestView.

Everything builds OK but my window is blank.

Is there anything obvious that I'm doing wrong?

Thanks, Alan

Upvotes: 0

Views: 347

Answers (1)

TheNextman
TheNextman

Reputation: 12566

It seems the logical approach but it won't work. You can't design a .xib, and then embed that directly in another .xib (like you would with, say, Windows Forms controls).

For this to work, you need to:

  • Add an outlet from your custom view to your window
  • In your code behind, create an instance of your view
  • Replace the custom view with your new instance programmatically, typically in AwakeFromNib()

Usually in Cocoa, you would use a ViewController to instantiate your test view instance, but it is not a requirement

this.Window.ContentView.ReplaceSubviewWith(this. myCustomViewPlaceHolder, new MyCustomViewController().View);

You may find this discussion helpful.

Upvotes: 2

Related Questions