What I'm trying to do:
My application is basically a user facing admin console for a database backend. There are several different types of 'data' that can be edited, and I want to change the view depending on what data the user is working on. I based my initial code on the ideas presented in Chapter 12 of Shane's book - Swapping Views, with a few changes:
• I programatically resize the NSViews to fit the window space available, rather than the other way around.
• I start with a blank main window and load in the main UI - seemed to make the switching back and forth easier.
• I'm animating the change
Details:
• I have a blank main window - it's Content view is bound to the mainUIFrame property in my script.
• I have the MainUI view, bound to mainUIView, which is immediately loaded into the main window.
• The MainUIView also has a table, which is bound via an NSArray to the testArray property in the main script.
If I have the table directly on the Main window, this works, but as soon as I put it onto an NSView and load that into place, it doesn't work. Further, if I load secondary array, THEN set my testArray to the second array, it also works.
ie: for the loop to create test data, if I do the following it works, but seems silly to be creating a ton of extra arrays:
set dummyList to {}
set x to 1
repeat while x < 50
set end of dummyList to {lCode:"Code" & x, lName:"Name" & x, lLocation:"Location" & x}
set x to x + 1
end repeat
set my testArray to dummyList
I even tried binding the table to the script so I could send it a reloadData() command, but that didn't work either.
Anyway, here is my simplified script:
property parent : class "NSObject"
property currentView : missing value -- I use this to keep track of which is the current view, for the replaceSubview:with: command
property mainUIFrame : missing value -- This is bound to the Content view of the Main window
property mainUIView : missing value -- This is bound to a custom NSView, which contains the main UI
property siteEditView : missing value -- This is bound to a second custom NSView, which contains a UI for editing Site data
property testArray : {} -- Test Array is bound to an NS Array controller, which is bound to a table.
on applicationWillFinishLaunching_(aNotification)
--Apparently I need to do this to get animation working...
mainUIFrame's setWantsLayer_(true)
--Load the main UI
changeMainView_(mainUIView)
--Create some simple data to test with
set x to 1
repeat while x < 50
set end of my testArray to {lCode:"Code" & x, lName:"Name" & x, lLocation:"Location" & x}
set x to x + 1
end repeat
end applicationWillFinishLaunching_
on changeMainView_(desiredView)
set currentSize to mainUIFrame's frame()
desiredView's setFrame_(currentSize)
--If this is the first time, the currentView will be null and I need to use addSubview instead of replaceSubview
if currentView is missing value then
mainUIFrame's addSubview_(desiredView)
else
mainUIFrame's animator's replaceSubview_with_(currentView, desiredView)
end if
--Update currentView for when I want to switch back
set currentView to desiredView
end changeMainView_
--These two functions are triggered by UI to switch views
on enterSiteEditor_(sender)
changeMainView_(siteEditView)
end enterSiteEditor_
on finishSiteEditing_(sender)
changeMainView_(mainUIView)
end finishSiteEditing_