Post

Replies

Boosts

Views

Activity

Reply to How to call function in NSViewController?
You are right it compiled fine with the reference and creating and instance, but did throw an exception due to the IBAction used some CustViewController properties. So, using notification I was able to get it working. Also, why do you think the delegation may be the cleanest way? Thanks for the help.
Topic: UI Frameworks SubTopic: AppKit Tags:
Aug ’22
Reply to How to see current window?
Maybe another way to accomplish what I want is...If when you change focus of a window an event is posted in the now focused window gets control. In other words can the focused window tell it just got the focus. Is there way to do this?
Topic: UI Frameworks SubTopic: AppKit Tags:
Aug ’22
Reply to How to add headers programmatically to NSTableView
I got the answer to this from apple support. It seems the tableView must part of a scrollView for the headers to appear. So I removed any reference to the explicit creation of a table header view and I added the following code to get this working: let scrollView = NSScrollView() scrollView.documentView = tableViewForPrint stackView.addArrangedSubview(scrollView)
Topic: UI Frameworks SubTopic: AppKit Tags:
Aug ’22
Reply to Drawing an NSImage using COCOA Drawing
Found the following code on gitHub extension NSImage {    /// Create a CIImage using the best representation available    ///    /// - Returns: Converted image, or nil    func asCIImage() -> CIImage? {       if let cgImage = self.asCGImage() {          return CIImage(cgImage: cgImage)       }       return nil    }    /// Create a CGImage using the best representation of the image available in the NSImage for the image size    ///    /// - Returns: Converted image, or nil    func asCGImage() -> CGImage? {       var rect = NSRect(origin: CGPoint(x: 0, y: 0), size: self.size)       return self.cgImage(forProposedRect: &rect, context: NSGraphicsContext.current, hints: nil)     } } extension CIImage {    /// Create a CGImage version of this image    ///    /// - Returns: Converted image, or nil    func asCGImage(context: CIContext? = nil) -> CGImage? {       let ctx = context ?? CIContext(options: nil)       return ctx.createCGImage(self, from: self.extent)    }    /// Create an NSImage version of this image    /// - Parameters:    ///   - pixelSize: The number of pixels in the result image. For a retina image (for example), pixelSize is double repSize    ///   - repSize: The number of points in the result image    /// - Returns: Converted image, or nil    #if os(macOS)    @available(macOS 10, *)    func asNSImage(pixelsSize: CGSize? = nil, repSize: CGSize? = nil) -> NSImage? {       let rep = NSCIImageRep(ciImage: self)       if let ps = pixelsSize {          rep.pixelsWide = Int(ps.width)          rep.pixelsHigh = Int(ps.height)       }       if let rs = repSize {          rep.size = rs       }       let updateImage = NSImage(size: rep.size)       updateImage.addRepresentation(rep)       return updateImage    }    #endif } extension CGImage {    /// Create a CIImage version of this image    ///    /// - Returns: Converted image, or nil    func asCIImage() -> CIImage {       return CIImage(cgImage: self)    }    /// Create an NSImage version of this image    ///    /// - Returns: Converted image, or nil    func asNSImage() -> NSImage? {       return NSImage(cgImage: self, size: .zero)    } } Added the following two lines to get what I needed  let cgimage: CGImage? = image.asCGImage() // Here comes the picture NSGraphicsContext.current?.cgContext.draw(cgimage!, in: dirtyRect)
Topic: UI Frameworks SubTopic: AppKit Tags:
Aug ’22
Reply to How to sort NSTableView Date Column?
I changed the code to convert date to an integer and that worked for me as well changed to a different column. let dateFormatter = DateFormatter()             dateFormatter.dateFormat = "YYYYMMdd"             dateFormatter.locale = Locale(identifier: "en_US_POSIX") invoices.sort { (p1, p2) -> Bool in                 let p1Date: Int? = Int(dateFormatter.string(from: p1.invoiceDateCreated!))                 let p2Date: Int? =  Int(dateFormatter.string(from: p2.invoiceDateCreated!))                 guard let id1 = p1Date, let id2 = p2Date else { return true }                 if ascending {                     return id1 < id2                 } else {                     return id2 < id1                 }             }
Topic: UI Frameworks SubTopic: AppKit Tags:
Sep ’22
Reply to NSPredicate format for date comparison
Either of the following two lines can cause the error: predicate = NSPredicate(format: "invoiceNumber contains %@ OR invoiceCustName contains %@ OR invoiceStatus contains %@ OR invoiceDateCreated >= %@",searchString,searchString,searchString,searchString) predicate = NSPredicate(format: "invoiceDateCreated >= %@", searchString as CVarArg) The problem I am having is with the operator >=, I not sure how to filter my table with the date type. The CONTAINS operator only works with strings. I want to be able to filter on a date type variable. Also, is this meant to say items? invoiceCount = "Found (items.count ) Invoices" ... No, it should say invoices - good catch
Topic: UI Frameworks SubTopic: AppKit Tags:
Sep ’22
Reply to NSPredicate format for date comparison
I figured this out. I had to create a date from the search string to use in the predicate>>> Here is the working code:     @objc func invoiceSearch(sender:NSSearchField) {   let searchString = sender.stringValue         let dateFormatter = DateFormatter()         dateFormatter.dateFormat = "YYYYMMdd"         var dateTemplate: String = "20000101"         var dateSearch: String = ""         var dateEndSearch: String = ""         dateFormatter.locale = Locale(identifier: "en_US_POSIX")         var predicate:NSPredicate = NSPredicate()         if searchString.isEmpty {             invoices = self.backUpInvoices         }         else{       // search field contains data             if searchString.isContainsLetters == false {     // If false must be a date search (Numerics or -)                 dateSearch = dateTemplate                    // Code to handle a begin and end date in                 if searchString.count < 9 {                  // the format YYYYMMDD-YYYYMMDD initially end date is                     dateSearch.removeFirst(searchString.count) // set to current date                     dateSearch = searchString + dateSearch                     searchBeginDate = dateFormatter.date(from: dateSearch)!                 }                 else {                     if searchString.count > 9 {                         dateSearch.removeFirst(searchString.count-9)                         dateEndSearch = searchString                         dateEndSearch.removeFirst(9)                         dateSearch = dateEndSearch + dateSearch                         searchEndDate = dateFormatter.date(from: dateSearch)!                     }                 }             }             if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "All" {                 predicate = NSPredicate(format: "invoiceNumber contains %@ OR invoiceCustName contains %@ OR invoiceStatus contains %@ OR invoiceDateCreated >= %@ AND invoiceDateCreated < %@",searchString,searchString,searchString,searchBeginDate as NSDate, searchEndDate as NSDate)             }             else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Number" {                 predicate = NSPredicate(format: "invoiceNumber contains %@",searchString)             }             else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Customer"{                 predicate = NSPredicate(format: "invoiceCustName contains %@",searchString)             }             else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "invoice Status"{                 predicate = NSPredicate(format: "invoiceStatus contains %@",searchString)             }             else if (invoiceSearchField.cell as? NSSearchFieldCell)?.placeholderString == "Invoice Date"{                 predicate = NSPredicate(format: "invoiceDateCreated >= %@ AND invoiceDateCreated < %@", searchBeginDate as NSDate, searchEndDate as NSDate)             }             invoices = (self.backUpInvoices as NSArray).filtered(using: predicate) as! [Invoice]         }
Topic: UI Frameworks SubTopic: AppKit Tags:
Sep ’22
Reply to How to Initialize a structure with in a class?
Thanks Claude, follow up question.. How do I populate ChartSeriesData? I thought I could just add the following: However coding this way there is no append method. extension ChartSeriesRow {     func addRow(chartRow: ChartSeriesData){          ChartSeriesRow.ChartSeriesData.chartEntry.append(contentsOf: chartRow)     } }
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’22
Reply to How to add an IBAction to a macOS tableView Cell
import Cocoa class CustomTableCell: NSTableCellView {     @IBOutlet weak var userNameLabel: NSTextField!     @IBOutlet weak var roleLabel: NSTextField!    @IBOutlet var testTextView: NSTextView!     override func draw(_ dirtyRect: NSRect) {         super.draw(dirtyRect)         // Drawing code here.     }     @IBAction func emailButton(_ sender: Any) {         // Email user     } } I was first trying to add a @IBAction for a scrollview and when I control drag the scrollview to the CustomTableCell file I could not create and @IBAction. It is like having a scrollview object inside a scrollable table view is not allowed. So, I was not able to create an action to get the contents of the scrollview in the ViewController code. Much less creating code to pass to the viewController the contents of the scrollView text entered. I sent a offline demo project to illustrate what I am saying. Thanks for your help on this.
Topic: UI Frameworks SubTopic: AppKit Tags:
Nov ’22