xcodemacosapplescriptappdelegatecocoa-bindings

MacOS: How to update an AppDelegate property via editing a table view cell?


In my macOS app I’m trying to update an AppDelegate property by editing a Table View Cell.

I created a sample project (Table View example project with array controller) using tips from the answer to this post.

I’m using macOS Big Sur (11.6.8) and Xcode 12.5.1. I'll be glad to supply a source archive download link or email it if requested. When I tried to add a link in this post it was rejected.

Here's my AppDelegate script:
    script AppDelegate
    property parent : class "NSObject"
    property msg : ""

    -- IBOutlets
    property theWindow : missing value
    property tableViewData : missing value
    property arrayController : missing value
    property showTableViewData : missing value
    
    on `applicationWillFinishLaunching_(aNotification)`     
    appInit()
    end applicationWillFinishLaunching_
    
    on applicationShouldTerminate_(sender)
    return current application's NSTerminateNow
    end applicationShouldTerminate_
    
    on appInit()
        
    set my msg to (current date & return & return & "Initializing…") as string
    delay .5
    initTableView()
    set my msg to (current date & return & return & "Ready.") as string
    delay .5
        
    end appInit
    
    on initTableView()
    
    set my tableViewData to {}
    set my tableViewData to {{adName:("C21 ad" as string), pageNumber:("001" as string)}, {adName:("ERA ad" as string), pageNumber:("002" as string)}}

    end initTableView
    
    on `showTableViewData_(sender)`

    set my msg to (current date & return & return & "showing tableViewData....") as string
    delay .5

    set displayText to ""
    repeat with thisRecord in tableViewData
            
    display alert "adName: " & (adName of thisRecord as  string) & return & "pageNumber: " & (pageNumber of thisRecord as  string)

    end repeat
        
    set my msg to (current date & return & return & "Ready.") as string
    delay .5

    end `showTableViewData_`

    end script

How it works

As you can see I have hardcoded two records which I use to populate the tableViewData property on initialization which in turn is displayed in the table view. For some reason the adName data is not being displayed, but that bug is not what this post is about.

App initialized

Clicking the button reads the tableViewData property, iterates the records displaying them via an alert (so we can see it's contents).

Record display

Aside from the adName not displaying, so far so good.

Next, I edit the pageNumber table view cell for the first record (changing it from "001" to "777" and hitting return). When I click the button the first record is displayed which still shows a pageNumber value of "001" (instead of the "777" value I entered).

Record display after editing

Here are some shots of the table view cell attributes, connections, and bindings:

Attributes Connections Bindings

I tried selecting the bindings setting "Continuously Updates Value" but that doesn't seem to help; my tableViewData property is not updated with the newly entered data in the table view cell.

Most of the current developer docs use Obj-C or Swift instead of AppleScript and iOS or tvOS related. I'm using Xcode 12.5.1 because of problems binding UI elements to my code.

Thanks for taking the time to look this over.


Solution

  • The contents of tableViewData doesn't change but content of arrayController does. Try

    repeat with thisRecord in content of arrayController
        display alert "adName: " & (adName of thisRecord as  string) & return & "pageNumber: " & (pageNumber of thisRecord as  string)
    end repeat
    

    To make it work, use a Script Object instead of a record.

    on makeAd(nameOfAd, pageNr)
        script ad
            property adName: nameOfAd
            property pageNumber: pageNr
        end script
        return ad
    end makeAd
    
    on initTableView()
        set my tableViewData to {makeAd("C21 ad", "001"), makeAd("ERA ad", "002")}
    end initTableView