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