Outlets are null after instantiating custom NSViewController with XIB

I'm in the process of trying to get a custom NSTableViewCell going. I have a custom NSTableViewSource that I set as the Source for my table view. I added a new "Cocoa View with Controller" to my project that will be my custom cell. I finally got it all working such that it's displaying, but now when I try to set some labels inside the Initialize method of the custom NSViewController the outlets are all null as if it's not properly loading the XIB or something. I'm not sure what's going on. Thoughts?

A bit from my custom NSViewController

public partial class WatchedFolderTableViewCellController : MonoMac.AppKit.NSViewController
{

    #region Constructors

    // Called when created from unmanaged code
    public WatchedFolderTableViewCellController (IntPtr handle) : base (handle)
    {
        Initialize ();
    }
    // Called when created directly from a XIB file
    [Export ("initWithCoder:")]
    public WatchedFolderTableViewCellController (NSCoder coder) : base (coder)
    {
        Initialize ();
    }
    // Call to load from the XIB/NIB file
    public WatchedFolderTableViewCellController () : base ("WatchedFolderTableViewCell", NSBundle.MainBundle)
    {
        Initialize ();
    }
    // Shared initialization code
    void Initialize ()
    {
        // lblDetails is null here
    }

A bit from my custom NSTableViewSource...

[Register ("WatchedFolderTableViewSource")]
public class WatchedFolderTableViewSource : NSTableViewSource
{
    public WatchedFolderTableViewSource ()
    {
    }

    public override int GetRowCount (NSTableView tableView)
    {
        return AppData.Instance.WatchedFolders.Count;
    }

    public override NSView GetViewForItem (NSTableView tableView, NSTableColumn tableColumn, int row)
    {
        WatchedFolderTableViewCellController controller = new WatchedFolderTableViewCellController ();
        return (WatchedFolderTableViewCell)controller.View;
    }

And the standard stuff in the designer file for the generated outlets...

[Register ("WatchedFolderTableViewCellController")]
partial class WatchedFolderTableViewCellController
{
    [Outlet]
    MonoMac.AppKit.NSButton btnSettings { get; set; }

    [Outlet]
    MonoMac.AppKit.NSTextField lblDetails { get; set; }

    [Outlet]
    MonoMac.AppKit.NSTextField lblFolderPath { get; set; }

    void ReleaseDesignerOutlets ()
    {
        if (lblDetails != null) {
            lblDetails.Dispose ();
            lblDetails = null;
        }

        if (lblFolderPath != null) {
            lblFolderPath.Dispose ();
            lblFolderPath = null;
        }

        if (btnSettings != null) {
            btnSettings.Dispose ();
            btnSettings = null;
        }
    }
}

[Register ("WatchedFolderTableViewCell")]
partial class WatchedFolderTableViewCell
{

    void ReleaseDesignerOutlets ()
    {
    }
}

Posts

  • rjmrjm CAMember ✭✭✭

    Outlets will always be null in the constructor, the .xib is not loaded yet. You should try overriding AwakeFromNib. Outlets will be initialised by the time that is called.

  • robertmiles3robertmiles3 USMember ✭✭

    @rjm

    Thanks. That works, just didn't realize you always had to do that. I guess in my case I'll have to have vars for the labels and just set the label values in AwakeFromNib.

  • tofutimtofutim USUniversity ✭✭

    Robert, how did you end up fixing this?

  • robertmiles3robertmiles3 USMember ✭✭

    @tofutim, I did was @rjm said...override AwakeFromNib and do my initialization there.

  • SteveFlackSteveFlack GBMember ✭✭

    Be aware that there are circumstances where AwakeFromNib can be called more than once - for example under certain set ups where you create a new view for a view based tableview from an xib.

  • MaxPavlovMaxPavlov USMember ✭✭
    edited January 2015

    Hi guys.

    Hope someone will see this related question in the thread and have an answer.

    I have An AppController create an NSWindow and show it. NSWindow creates NSView with View's NSViewController and puts it inside of window via a call to AddSubview.

    AppController tells NSWindow instance via a custom method to put some text in an NSTextField.StringValue right away. But I assume, the NSView AwakeFromNib doesn't happen yet at his point, so telling a ViewController to set the Outlet value (NSTextField is linked to View's Controller as an Outlet), doesn't update the text field's value.

    What is the pattern to wait for AwakeFromNib for child View by the window and then modify it's outlets?

    Thanks.

    P.S. False alarm. My Outlet wasn't being updated, because I was locking the UI thread with Sleep. As long as you change Outlet after AwakeFromNib, it updates on the screen.

Sign In or Register to comment.