How to pass value from NSViewController to custom NSView of NSPopover?

By using the delegation protocol I have tried to pass a string (inputFromUser.string) from NSViewController - mainController to custom subclass of NSView of NSPopover - PlasmidMapView, to drawRect function (to draw a complex circle in dependence on the text typed by user in NSTextView declared as @IBOutlet in mainController), see code below. But, it didn’t work. I don’t know where a mistake is. I have tried to find the answer for several days and I am exhausted !!! I need a solution and I guess it should be a standard in Cocoa. If not, maybe there is another than delegation way to pass this string?

File 1.

  protocol PlasmidMapDelegate 
 {
    func giveDataForPLasmidMap(dna: String)
 }

  class MainController: NSViewController 
 {

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

    @IBAction func actionPopoverPlasmidMap(sender: AnyObject)
    {
         let dna = inputDnaFromUser.string
         delegate?.giveDataForPLasmidMap(dna!)  

         popoverPlasmidMap.showRelativeToRect(sender.bounds, 
             ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)
    }
}

File 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 inputFromUser.string 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()
    }
 }

Looking at that code, I felt all my bones shudder.

Those two lines of code don’t belong there. Get rid of those. And get rid of the delegate.

Just set the value of dnaForMap in MainController immediately before you display the popover view.

I am sorry, I don’t understand. Do you suggest not to use delegation ? Please could you show exactly where I have to set the value of dnaForMap ?

I have removed the code for delegation and changed the @IBAction func actionPopoverPlasmidMap(sender: AnyObject)

to following

@IBAction func actionPopoverPlasmidMap(sender: AnyObject)
{
    PlasmidMapView().dnaForMap = inputDnaFromUser.string!

    popoverPlasmidMap.showRelativeToRect(sender.bounds, ofView: sender as! NSView, preferredEdge: NSRectEdge.MinY)
}

But, it is not working, dnaForMap.characters.count is 0 after typing some text in inputDnaFromUser NSTextView window.

Looks like you have just dived into Coca Programming without any preparation.

If I showed you exactly what to do, I won’t be helping you at all.

You need to learn the basics first.

Here is a list of things can do:

  1. Read BNR’s Cocoa Programming for OS X book and make sure that you understand the concepts.
  2. Do Apple tutorials for programming on OS X.
  3. Learn how to use a TableView to become familiar with using delegates.

Looks like you have just dived into Coca Programming without any preparation.
If I showed you exactly what to do, I won’t be helping you at all.

I had needed a concrete adviсe, not to listen a concept what and how to do. BTW, your concept of teaching is wrong. I had found a solution.

This is one solution (there is one more, even shorter).

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)         
    }
}