Home > Net >  Argument type 'ContentView' expected to be an instance of a class or class-constrained typ
Argument type 'ContentView' expected to be an instance of a class or class-constrained typ

Time:06-23

I want to trigger a function that's in StatusBar Controller with a button in Content view which hides or shows a menubar popover but i'm getting this error:

Argument type 'ContentView' expected to be an instance of a class or class-constrained type

StatusBar Controller

..........
init(_ popover: NSPopover)
{
    self.popover = popover
    statusBar = NSStatusBar.init()
    statusItem = statusBar.statusItem(withLength: 28.0)
    
    if let statusBarButton = statusItem.button {
        statusBarButton.image = #imageLiteral(resourceName: "StatusBarIcon")
        statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
        statusBarButton.image?.isTemplate = true
        
        statusBarButton.action = #selector(togglePopover(sender:))
        statusBarButton.target = self
    }
    
    eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown], handler: mouseEventHandler)
}

 

 @objc func showPopover(sender: AnyObject) {

  if(popover.isShown) {
        hidePopover(sender)
    }
    else {
        displayPopover()
    }

}

  func displayPopover() {
    if let statusBarButton = statusItem.button {
        popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)
        eventMonitor?.start()
    }
}
   func hidePopover(_ sender: AnyObject) {
        popover.performClose(sender)
        eventMonitor?.stop()
    }



   func mouseEventHandler(_ event: NSEvent?) {
        if(popover.isShown) {
            hidePopover(event!)
        }
    }

ContentView

       var statusBar: StatusBarController?

        Button("Show/Hide Popover "){
        statusBar?.showPopover(sender: self)        
        }
       

CodePudding user response:

Just give it nil sender, like

Button("Show/Hide Popover "){
   statusBar?.showPopover(sender: nil)  // << here !!        
}

and make all controller actions with optional sender, like

 @objc func showPopover(sender: AnyObject?) {
 // ...
  • Related