PDA

View Full Version : indexed objects in cocoa




lostingamma
Apr 5, 2005, 08:49 AM
is there some way in cocoa to have many diffrent NSImageView objects, and give them a diffrent index? in applescript, i could just do:

set contents of image view i to (item theAnswer of theSymbols)

but, in cocoa, u would have to set he outlets, which im not sure how you would do this cocoa, so, that i could referrer to a object in a method



robbieduncan
Apr 5, 2005, 09:12 AM
You seem to have got very confused. Why do you want multiple NSImageViews. Can you not have a NSArray of NSImages (getting you your indexed access) and set the image view to show the one you want (which is pretty much what your AppleScript seems to do)?

lostingamma
Apr 5, 2005, 09:28 AM
well, there is a image view for each image, and certain images go into certain image views, and if i use and index i can get it so that one image goes into all the image wells that are used for numbers that are divisble by 9, then, the applescriptcode fills in all the other image views with other random images, that way, there is an image next to each number (which is a system text label thing) so that the same image appears next to all the numbers divisble by 9, and the applescript code does this by using indexs of the image views, so that it can refrence to the correct image view just by using an integer, becuase that integer changes while it goes through a repeate statement, here is more of the code:

if item 1 of theSymbols is null then --load images if they haven't aleady
repeat with i from 1 to 25
set item i of theSymbols to (load image (i as string) & " copy")
end repeat
end if
if imagesRandomized is false then --shuffle images if they havn't already
load sound "next"
set theAnswer to random number from 1 to 25
tell window windowName
tell drawer "drawer"
repeat with i from 100 to 1 by -1
if ((i - 1) / 9) mod 1 is 0 then
set contents of image view i to (item theAnswer of theSymbols)
else
set theRandom to random number from 1 to 25
if theRandom is equal to theAnswer then
set theOther to random number from 1 to 2
if theOther is equal to 1 then
set contents of image view i to (item theAnswer of theSymbols)
else
set contents of image view i to (item (random number from 1 to 25) of theSymbols)
end if
else
set contents of image view i to (item theRandom of theSymbols)
end if
end if
end repeat
end tell
set imagesRandomized to true
end tell
end if


the code works fine in applescript, im just trying to recreate the program in cocoa

robbieduncan
Apr 5, 2005, 10:07 AM
well, there is a image view for each image, and certain images go into certain image views, and if i use and index i can get it so that one image goes into all the image wells that are used for numbers that are divisble by 9, then, the applescriptcode fills in all the other image views with other random images, that way, there is an image next to each number (which is a system text label thing) so that the same image appears next to all the numbers divisble by 9, and the applescript code does this by using indexs of the image views, so that it can refrence to the correct image view just by using an integer, becuase that integer changes while it goes through a repeate statement, here is more of the code:



the code works fine in applescript, im just trying to recreate the program in cocoa

Any chance of sentances going forward? Punctuation, like comments in code, make it much easier to read and understand what you are talking about! Once I get home I'll post an XCode project that creates a NSArray of NSImageViews for you...

lostingamma
Apr 5, 2005, 11:27 AM
well, in short, you don't really need an array, atleast, i don't think so. In short, here is an example of what I want to do.

You have 3 textfields.
The outlets are: textField0, textField1, textField2

you would right code sort of like this:

int i; //define the variable
for (i = 0; i < 3; i++) { //set a for statment, that will make i 0 to 2
[textField(i) setObjectValue:i];
}

so, basicly, the i needs to be used to define the textfield. There is no "textField" , all the text fields have numbers after them, and that is what "i" is for. So, the output would be:

textField0 would have the text "0"
textField1 would have the text "1"
textField2 would have the text "2"

If I could figure out how to do that, I could do it in my code. The problem I am having is the "[textField(i)" part. I know that that's not how you would do it, I am not sure how to do it though. I can't find any examples of what I want to do in any books that I have.

robbieduncan
Apr 5, 2005, 12:28 PM
...
If I could figure out how to do that, I could do it in my code. The problem I am having is the "[textField(i)" part. I know that that's not how you would do it, I am not sure how to do it though. I can't find any examples of what I want to do in any books that I have.

You can't do that. That's why there are no examples of it! Indexed access is for arrays (either of the normal C type or NSArrays). You cannot do that sort of string interpolation in C or Obj-C.

Assuming that you have added all your controls to a container view (or the window) then you can do something like:


IBOutlet NSView *view

....

int count ;
int found = 0;
NSArray *subviews = [view subviews];
NSView *view;
for (i=0;i<[subviews count];i++)
{
view = [subviews objectAtIndex:i];
if ([view isKindOfClass:[NSImageView class])
{
// Set the image via found index here
[view setImage:....];
found++;
}
}

...