Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Objective-C/AppleScriptObjC | Outlet Collection

Hello,


Basically I have a bunch of variables in my project, whose names are identical, only a number is different. I have to write a code, which is almost the same for all the variables, but a number changes. The actual problem is that all these variables are outlet connected to objects in IB.

Example in Objective-C:


-(IBAction)setImages:(id)sender {

   int key = (int)[sender tag];
   if (key == 1) {
     [imageView1 setImage:myImage];
   } else if (key == 2) {
     [imageView2 setImage:myImage];
   } else if (key == 3) {
     [imageView3 setImage:myImage];
   }

}


AppleScriptObjC:


on setImages:sender

   set key to sender's tag()
   if  key = 1 then
      imageView1's setImage:myImage
   else if key = 2 then
      imageView2's setImage:myImage
   else if key = 3 then
      imageView3's setImage:myImage
   end if

end setImages:


This would continue all the way up to 16, so I would like something like an Outlet Collection in Objective-C for iOS:


-(IBAction)setImages:(id)sender {

   int key = (int)[sender tag];
   [imageView(key) setImage:myImage];


}


AppleScriptObjC:


on setImages:sender

   set key to sender's tag()
   imageView(key)'s setImage:myImage

end setImages:


Thanks in advance for the replies!

Posted on Jul 2, 2015 1:11 AM

Reply
Question marked as Best reply

Posted on Jul 3, 2015 12:26 AM

No longer need help, I used this code:

Objective-C

-(IBAction)setImages:(id)sender {
   int tag = (int)[sender tag];
   NSString* keyPath = [NSString stringWithFormat:@"imageView%d.image", tag];
   [self setValue:myImage forKeyPath:keyPath];

}


AppleScriptObjC

on setImages:sender       
    set tag to sender's tag as integer
    set keyPath to (current application's NSString's stringWithFormat:("imageView" & tag & ".image"))
    my setValue:myImage forKeyPath:keyPath    
end setImages:
1 reply
Question marked as Best reply

Jul 3, 2015 12:26 AM in response to Matteo_999

No longer need help, I used this code:

Objective-C

-(IBAction)setImages:(id)sender {
   int tag = (int)[sender tag];
   NSString* keyPath = [NSString stringWithFormat:@"imageView%d.image", tag];
   [self setValue:myImage forKeyPath:keyPath];

}


AppleScriptObjC

on setImages:sender       
    set tag to sender's tag as integer
    set keyPath to (current application's NSString's stringWithFormat:("imageView" & tag & ".image"))
    my setValue:myImage forKeyPath:keyPath    
end setImages:

Objective-C/AppleScriptObjC | Outlet Collection

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.