【问题标题】:Is there an event or method that is called after a NSCollectionViewItem is created to get or set the view controller?在创建 NSCollectionViewItem 以获取或设置视图控制器后,是否有调用事件或方法?
【发布时间】:2021-08-24 03:01:17
【问题描述】:

我想从 NSCollectionViewItem 内部获取对主应用程序视图控制器的引用。

我创建了这个函数:

public partial class MyViewItemController : NSCollectionViewItem {

    private ViewController controller;

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

    partial void buttonAction(Foundation.NSObject sender) {
        var button = sender as NSButton;
        controller.performSomething(model);
    }

    private ViewController getController() {
        var localDelegate = CollectionView.Delegate as CollectionViewDelegate;
        var controller = localDelegate.ParentViewController as ViewController;
        return controller;
    }
}

但是如果我在 Initialize 方法中调用它会引发错误:

    private ViewController controller;

    // Shared initialization code
    void Initialize() {
        controller = getController();// Failed to lookup the required marshalling information.
    }

从错误看来,应用程序生命周期中的工作还为时过早。

我是否可以通过其他方式在 View Item 类中获取视图控制器,或者我可以覆盖的任何其他方法,而不是可以设置控制器变量的 Initialize()?

【问题讨论】:

  • 在主应用程序视图控制器中实现按钮操作并将按钮连接到第一响应者。

标签: swift objective-c macos xamarin nscollectionview


【解决方案1】:

你应该能够向你的视图询问它的窗口,然后从那里询问主窗口

NSWindow *mainWindow = self.window.mainWindow;
//mainWindow.windowController
//mainWindow.contentViewController

您可能需要检查您的用例的控制器是什么类。

【讨论】:

  • 这是因为没有窗口没有哭泣.. 也就是没有视图。
猜你喜欢
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2010-10-30
  • 2012-12-07
  • 1970-01-01
相关资源
最近更新 更多