swift – OSX:基于视图的表视图中的对象可能只能连接到表视图的委托

swift – OSX:基于视图的表视图中的对象可能只能连接到表视图的委托,第1张

概述我已经设置了一个带有嵌入式NSTableView的NSView. 我试图为对表视图单元格进行更改时运行NSTableViewCell的 *** 作: import Cocoaclass MyView: NSView{ override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) } 我已经设置了一个带有嵌入式NStableVIEw的NSVIEw.

我试图为对表视图单元格进行更改时运行NStableVIEwCell的 *** 作:

import Cocoaclass MyVIEw: NSVIEw{    overrIDe func drawRect(dirtyRect: NSRect)    {        super.drawRect(dirtyRect)    }    @IBAction func valEntered2(sender: AnyObject)    {        Swift.print("valueEntered2")    }}

虽然这种方法在故事板上使用NSVIEwController之前已经运行良好,但是当我编译此代码时它会发出警告:

由’table VIEw Cell’发送的动作’valEntered2:’连接到’VIEw’,一个无效的目标(基于视图的对象内部表视图,它们只连接到表视图的委托.)

所以它似乎不喜欢NSVIEw中的动作所以我已经将NStableVIEw子类化了,所以我可以将它移动到那里:

class Mytable: NStableVIEw,NStableVIEwDataSource,NStableVIEwDelegate{    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate    overrIDe init(frame frameRect: NSRect)    {        super.init(frame: frameRect)    }    required init?(coder: NSCoder)    {        super.init(coder: coder)        self.setDataSource(self)        self.setDelegate(self)    }    overrIDe func drawRect(dirtyRect: NSRect)    {        super.drawRect(dirtyRect)    }    func numberOfRowsIntableVIEw(tableVIEw: NStableVIEw) -> Int    {        return 5    }    func tableVIEw(tableVIEw: NStableVIEw,vIEwFortableColumn tableColumn: NStableColumn?,row: Int) -> NSVIEw?    {        let cell: NStableCellVIEw = tableVIEw.makeVIEwWithIDentifIEr(tableColumn!.IDentifIEr,owner: self) as! NStableCellVIEw        cell.textFIEld!.stringValue = "Hello World"        return cell    }}

我已在视图中设置表以使用此类:

我本来希望这是这个NStableVIEw的委托,并能够将tableVIEwCell的动作添加到Mytable,但它拒绝创建动作.

我错过了哪些步骤?

首先,你必须在tableVIEw单元格中添加一个委托
import Cocoaprotocol cellAction {  func CellAction()}class MyVIEw: NSVIEw{var delegate:cellAction!    overrIDe func drawRect(dirtyRect: NSRect)    {        super.drawRect(dirtyRect)    }    @IBAction func valEntered2(sender: AnyObject)    {      delegate.CellAction()        Swift.print("valueEntered2")    }}

than after that you have to add delegate to the table vIEw

class Mytable: NStableVIEw,NStableVIEwDelegate,cellAction{    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate    overrIDe init(frame frameRect: NSRect)    {        super.init(frame: frameRect)    }    required init?(coder: NSCoder)    {        super.init(coder: coder)        self.setDataSource(self)        self.setDelegate(self)    }    overrIDe func drawRect(dirtyRect: NSRect)    {        super.drawRect(dirtyRect)    }    func numberOfRowsIntableVIEw(tableVIEw: NStableVIEw) -> Int    {        return 5    }    func tableVIEw(tableVIEw: NStableVIEw,owner: self) as! NStableCellVIEw        cell.textFIEld!.stringValue = "Hello World"       cell.delegate = self        return cell    }}

in that class you have to call a method in delegate

func CellAction(){ //here you get the call back }
总结

以上是内存溢出为你收集整理的swift – OSX:基于视图的表视图中的对象可能只能连接到表视图的委托全部内容,希望文章能够帮你解决swift – OSX:基于视图的表视图中的对象可能只能连接到表视图的委托所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1030679.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存