Andy Fedoroff Andy Fedoroff - 1 month ago 18
Swift Question

Connect a NSLabel to CGPoint dragged with mouse


I have several rectangles made from CGPoints (Xcode 7.3, Swift 2.2). I want to create a label showing a number for any selected rectangle. So I made AppDelegate for array of CGPoints. But at runtime I see the label showing just 0 and it stands still.

I suspect my problem stems from
applicationDidFinishLaunching
method that doesn't allow update a label when I add new rectangles, or it depends on principle of delegation what is possibly wrong.

How to make a label showing an index for any selected rectangle?


enter image description here

Code in DrawView.swift :

class DrawView: NSView {

..................

let delegate = NSApplication.sharedApplication().delegate as! AppDelegate

delegate.appArray = myArray.flatMap { $0.coord() }
}


Code in AppDelegate.swift :

class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!
@IBOutlet weak var drawingView: DrawView!
@IBOutlet weak var label: NSTextField!

var appArray: [CGPoint] = []

func applicationDidFinishLaunching(aNotification: NSNotification) {

label.textColor = NSColor(calibratedRed: 0.85, green: 0, blue: 0.05, alpha: 0.7)
label.font! = NSFont(name: "Arial Bold", size: 60)!
label.backgroundColor = NSColor.clearColor()

dispatch_async(dispatch_get_main_queue(), {
self.label.stringValue = "\(self.appArray.count/4)"
})

for pointIndex in 0..<appArray.count {

let point = CGPoint(x:(appArray[pointIndex].x), y:(appArray[pointIndex].y))

label.sizeToFit()

label.frame = CGRect(origin: point, size:
CGSize(width: label.bounds.width, height: label.bounds.height))
}
}
}

Answer

Finally I found a solution. Wrong choice of applicationDidFinishLaunching method affected the performance of my dynamic label. So I changed the method to applicationWillUpdate. Now everything works as intended. Here is my corrected code:

AppDelegate.swift :

class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow! 
@IBOutlet weak var drawingView: DrawView! 
@IBOutlet weak var label: NSTextField!

var appArray: [CGPoint] = []

    func applicationWillUpdate(aNotification: NSNotification) { 

        label.textColor = NSColor(calibratedRed: 0.15, green: 0, blue: 0.75, alpha: 0.3)
        label.font! = NSFont(name: "Arial Bold", size: 60)!   
        label.backgroundColor = NSColor.clearColor()

        dispatch_async(dispatch_get_main_queue(), {   
            if self.appArray.count != 0 {
            self.label.stringValue = "\(self.appArray.count/4)"
            } else {
            self.label.stringValue = ""
            }
        })

            for pointIndex in 0..<(appArray.count/4) {

                let point = CGPoint(x:(appArray[pointIndex * 4].x), y:(appArray[pointIndex * 4].y)) 

                label.sizeToFit()

                label.frame = CGRect(origin: point, size: CGSize(width: label.bounds.width, height: label.bounds.height))
            } 

    label.wantsUpdateLayer    

    }
}
Comments