-1

In my Project I have a View with Controller called TaskOverviewView with just a NSTextField, which I'd like to use like a widget. In a NSWindow I have a NSCustomView and a NSScrollView as well. On booth I try to add a instance of the view of TaskOverviewViewController as a Subview using the addSubview(NSView) method. While it works on the ScrollView the widget isn't visible on the CustomView instead.

Code:

Code of the TaskOverviewViewController:

using System;
using System.Collections.Generic;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;

    namespace GetTheMilk.MacGUI

{
    public partial class TaskOverviewViewController : MonoMac.AppKit.NSViewController
    {
        #region Constructors

        // Called when created from unmanaged code
        public TaskOverviewViewController (IntPtr handle) : base (handle)
        {
            Initialize ();
        }

        // Called when created directly from a XIB file
        [Export ("initWithCoder:")]
        public TaskOverviewViewController (NSCoder coder) : base (coder)
        {
            Initialize ();
        }

        // Call to load from the XIB/NIB file
        public TaskOverviewViewController () : base ("TaskOverviewView", NSBundle.MainBundle)
        {
            Initialize ();
        }

        // Shared initialization code
        void Initialize ()
        {
            _taskName = new NSTextField();
            _taskName.StringValue = "HUHUH";
        }

        #endregion

        //strongly typed view accessor
        public new TaskOverviewView View {
            get {
                return (TaskOverviewView)base.View;
            }
        }
    }
}

the Controller of my StartupWindow:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;

namespace GetTheMilk.MacGUI
{
    public partial class StartupWindowController : MonoMac.AppKit.NSWindowController
    {
        #region Constructors

        // Called when created from unmanaged code
        public StartupWindowController (IntPtr handle) : base (handle)
        {
            Initialize ();
        }

        // Called when created directly from a XIB file
        [Export ("initWithCoder:")]
        public StartupWindowController (NSCoder coder) : base (coder)
        {
            Initialize ();
        }

        // Call to load from the XIB/NIB file
        public StartupWindowController () : base ("StartupWindow")
        {
            Initialize ();
        }

        // Shared initialization code
        void Initialize ()
        {

        }

        #endregion

        //strongly typed window accessor
        public new StartupWindow Window {
            get {
                return (StartupWindow)base.Window;
            }
        }

        partial void _ButtonClick(NSObject sender)
        {
            _Label1.StringValue = "Hello";
            TaskOverviewViewController widget = new TaskOverviewViewController();
            _cview.AddSubview(widget.View);
            _scrillView.AddSubview(widget.View);
        }
    }
}
john84
  • 2,431
  • 4
  • 24
  • 30

1 Answers1

0

The Problem is not related to ScrollView and CustomView. instead, it correlates with the call order. If I'd switch it instead the ScrollView won't work, but the CustomView. The solution is Correlated with a problem, I asked here: NullReferenceException when accessing GUI Components in Controllers constructor

Instead of putting the code into the Initialize() Method I should drop it into the AwakeFromNib() Method.

Community
  • 1
  • 1
john84
  • 2,431
  • 4
  • 24
  • 30