indexed objects in cocoa

Discussion in 'Mac Programming' started by lostingamma, Apr 5, 2005.

  1. macrumors member

    Joined:
    Apr 4, 2005
    #1
    is there some way in cocoa to have many diffrent NSImageView objects, and give them a diffrent index? in applescript, i could just do:

    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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)?
     
  3. thread starter macrumors member

    Joined:
    Apr 4, 2005
    #3
    well

    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
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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...
     
  5. thread starter macrumors member

    Joined:
    Apr 4, 2005
    #5
    well

    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.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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:

    Code:
    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++;
     }
    }
    
    ...
    
     

Share This Page