Thank you so much! I will need to take tonight to work on this but I
so appreciate the time you took to explain in such great detail. I am
a longtime web app/database programmer that made the jump to XCode -
and I am in love with it but have been staring blankly at a lot of
the IB options - one of them being the bindings tab. Your solution
seems like the way to go very slick. I didn't have a clue how to use
them - and I had never heard of User Defaults.
The reason I was deleting the menu items was because in IB you have
to have at least one default menu item in your popup button. But
since my values were coming from MySQL I needed to delete the default.
Thanks again, your input has opened up a new area of options and
excitement. I am ordering "The Mac Xcode 2 Book" and am hoping to get
some Cocoa under my belt in the near future too.
Chris
On Jul 8, 2005, at 10:42 AM, Andreas Kiel wrote:
Hi,
Unfortunately my replies do bounce often since I'm on Tiger. So no
idea whether or who got it.
@ Chris:
I'm not sure whether I understand why you fill the popup and delete
the menus.
Depending on what you want to do maybe the following is an
alternative. I did that with some of of my apps and does seem to
work fine and also requires only a little bit of coding and the
code is very easy to debug even with a lot buttons.
Use user defaults and Cocoa Bindings in IB. Just a simple example
for one popup. (Names in UD and Cocoa Bindings are case sensitive).
In IB select the popup and press "command-4" for the bindings.
Select "content values" and "Shared User Defaults" and hit return -
now you have connected the content of the popup with list or value
from your user defaults. But there is no real path to the value
yet. So type a name into the field "Model Key Path" like
"popup1Content"
Then you have to take care of which item is selected. In bindings
select either "selectedIndex" (or even maybe "selectedTag" , I
haven't tried that), hit return again and set the "Model Key Path"
to "popup1Index" or whatever.
You now have a popup which is automatically filled with values
based on a (yet undefined) list, and has a special (yet undefined)
index selected.
So there is the need to type some code in Xcode.
You have to create a basic list in User Defaults, so that the popup
finds the above described value list, also there is a index or
value needed. Best place is "on will finish launching", so the code
could look like for one button (with many buttons you can do a
repeat loop)
on will finish launching theObject
-- the below is for the first launch of the app
tell user defaults
make new default entry at the end of default entries with
properties {name:"popup1Content", value:{""}}
make new default entry at the end of default entries with
properties {name:"popup1Tags",value: {"0"}
make new default entry at the end of default entries with
properties {name:"popup1Index", value:0}
end tell
-- if you want to reset it with every launch type the below or
clear the entries any time during runtime
tell user defaults
set content of default entry "popup1Content" to {""}
set content of default entry "popup1Tags" to {""}
set content of default entry "popup1Index" to 0
end tell
end will finish launching
When launching your app the popup will have one entry - "" - which
is selected. In case you would have done:
tell user defaults
make new default entry at the end of default entries with
properties {name:"popup1Content",value: {"ABC","QRS","XYZ"}}
make new default entry at the end of default entries with
properties {name:"popup1Tags",value: {"32433","26554","55456"}}
make new default entry at the end of default entries with
properties {name:"popup1Index",value: 1}
...
...
end
the popup will have 3 entries - ""ABC","QRS","XYZ"" - where " QRS"
which is selected.
You can now always reset or fill the default entries during runtime
and the popup will reflect that like
set content of default entry "popup1Content" of user defaults to
{"1","2","3"}
or
set content of default entry "popup1Content" of user defaults to
myList -- myList = a list of text items
To make it easier to work with the records you can set up a data
source at launch time, like
set theDataSource to make new data source at the end of data sources
tell theDataSource
make new data column at the end of data columns with properties
{name:"the_vendor"}
make new data column at the end of data columns with properties
{name:"id_vendor"}
end
to get the values during runtime you could use for example (for the
text field, popup)
on action theObject
if name of theObject is "id_product" then
set id_product to content of theObject
set myParams to {id_product: id_product }
tell application "http://tm-myserver.local/xml-rpc/
product_images.lasso"
set the_data to call xmlrpc {method name:"fetch_product_vendors",
parameters:the_params}
end tell
-- then clear all data rows of the data source
delete every data row of theDataSource
-- then append your records
append theDataSource with the_data
-- now rebuild the lists
tell theDataSource
set popup1Content to data cell 1 of every data row
set popup1Tags to data cell 2 of every data row
end
-- now rebuild the UD entries
tell user defaults
set content of default entry "popup1Content" to popup1Content
set content of default entry "popup1Tags" to popup1Tags -- in case
you haven't set up the list as a property
set content of default entry "popup1Index" to 0 -- selects the
first vendor
end
else -- it's the popup
set myTag to item ((content of theObject)+1) of popup1Tags -- or
content of the UD value, s.a.
end
Hopefully I did not made to many typing errors and my english is
good enough to explain.
The advantage of the bindings is that you don't have to care about
structures of the UI and it very often helps to type less or at
least more simple code. In the above example you could have a
second popup which displays the id and connect it to the UD as
well, using the same index entry, but the tag list as content
values. Selecting a tag value in this popup will set the selected
value of the first popup to the right vendor, selecting a vendor in
first one will select the correct id in the second.