【问题标题】:How to pass value from NSViewController to custom NSView of NSPopover?如何将值从 NSViewController 传递到 NSPopover 的自定义 NSView?
【发布时间】:2016-05-13 07:40:04
【问题描述】:

通过使用委托协议,我尝试将字符串 (inputFromUser.string) 从 NSViewController - ma​​inController 传递到 NSPopover 的 NSView 的自定义子类 - PlasmidMapView ,要绘制矩形函数,请参见下面的代码。但是,它没有用。我不知道哪里错了。也许还有另一种方法可以传递这个字符串。

更新

文件 1。

  protocol PlasmidMapDelegate {
    func giveDataForPLasmidMap(dna: String)
  }

  class MainController: NSViewController {

    @IBOutlet var inputFromUser: NSTextView!
    var delegate: plasmidMapDelegate?

    @IBAction func actionPopoverPlasmidMap(sender: AnyObject) {
         popoverPlasmidMap.showRelativeToRect(sender.bounds, 
             ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)

         let dna = inputDnaFromUser.string
         delegate?.giveDataForPLasmidMap(dna!)  
    }
}

文件 2

class PlasmidMapView: NSView, PlasmidMapDelegate {

    var dnaForMap = String()

    func giveDataForPLasmidMap(dna: String) {
        dnaForMap = dna
    }       

    override func drawRect(dirtyRect: NSRect) {

    let objectOfMainController = MainController()
    objectOfMainController.delegate = self

        //here I have checked if the string dnaForMap is passed
         let lengthOfString = CGFloat(dnaForMap.characters.count / 10)

         let pathRect = NSInsetRect(self.bounds, 10, 45)
         let path = NSBezierPath(roundedRect: pathRect, 
             xRadius: 5, yRadius: 5)
         path.lineWidth = lengthOfString //the thickness of the line should vary in dependence on the number of typed letter in the NSTextView window - inputDnaFromUser
         NSColor.lightGrayColor().setStroke()
         path.stroke()
    }
 }

【问题讨论】:

    标签: swift cocoa nsview delegation nsviewcontroller


    【解决方案1】:

    好的,存在一些架构错误。您根本不需要委托方法和协议。您只需要定义明确的 setter 方法:

    我。将您的 PlasmidMapView 放入 NSViewController-子类。此视图控制器必须设置为您的NSPopover-control 的contentViewController-property。不要忘记在viewDidLoad-method 或其他方法中按照您需要的方式设置它。

    class PlasmidMapController : NSViewController {
        weak var mapView: PlacmidMapView!
    }
    

    二。在您的PlacmidMapView 中,不要忘记在dna 上调用needsDisplay-method 设置:

    class PlasmidMapView: NSView {
        //...
        var dnaForMap = String() {
            didSet {
                needsDisplay()
            }
        //...
    }
    

    三。随时从MainController-class 中设置dna-string。

    @IBAction func actionPopoverPlasmidMap(sender: AnyObject) {
         popoverPlasmidMap.showRelativeToRect(sender.bounds, 
             ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)
    
         let dna = inputDnaFromUser.string
         if let controller = popoverPlasmidMap.contentViewController as? PlasmidMapController {
             controller.mapView.dna = dna
         } else {
             fatalError("Invalid popover content view controller")
         }
    }
    

    【讨论】:

    • 有一点不清楚(我是初学者)-“不要忘记在 viewDidLoad 方法或其他方法中按照您需要的方式设置它”。你能解释一下吗?
    • @VYT 你需要初始化 mapView 对象,添加到 NSViewController 视图,设置自动布局约束等等。
    • 终于!!!我已经按照您的建议进行了尝试,并找到了一个简单的解决方案。这不完全是你的建议,但你给了我一个想法,我给你赏金。再次感谢!
    • @VYT 如果我的回答确实帮助您检查它是否正确,请。
    【解决方案2】:

    为了使用委托,你的类 PlasmidMapView 需要有一个 MainController 的实例(顺便说一句,命名约定是 Class,而不是 class)并符合 PlasmidMapDelegate(再次命名约定规定它应该是 PlasmidMapDelegate)。使用该实例,您可以:

    mainController.delegate = self

    【讨论】:

    • 如果我将 mainController.delegate = self 放在 PlasmidMapView 类中,我会抱怨 - 预期声明。
    • PlasmidMapView 是 MainController 的子视图吗?
    • 不,是NSPopover的NSView
    【解决方案3】:

    因此,几天后,我找到了一个没有任何协议和授权的解决方案,正如 Astoria 所提到的。我需要做的就是在 MainController 类中为我的自定义 NSView 创建@IBOutlet var plasmidMapIBOutlet: PlasmidMapView!,然后使用它在@IBAction func actionPopoverPlasmidMap(sender: AnyObject) 中设置 dnaForMap 的值。

    class PlasmidMapView: NSView
    {
        var dnaForMap = String()
    }
    
    class MainController: NSViewController 
    {
        @IBOutlet var inputFromUser: NSTextView!
        @IBOutlet var plasmidMapIBOutlet: PlasmidMapView!      
    
         @IBAction func actionPopoverPlasmidMap(sender: AnyObject) 
        {
             plasmidMapIBOutlet.dnaForMap = inputDnaFromUser.string!  
    
             popoverPlasmidMap.showRelativeToRect(sender.bounds, 
             ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)         
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-02
      • 2020-06-13
      • 1970-01-01
      • 2018-12-11
      • 2013-07-13
      • 2020-09-06
      • 2017-09-28
      • 1970-01-01
      相关资源
      最近更新 更多