How to Use Iboutletcollection to Connect Multiple Uiimageviews to the Same Outlet

How can I use IBOutletCollection to connect multiple UIImageViews to the same outlet?

Declare a property to hold your imageView's and then hook them up in interface builder like normal

@property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;

it's just a normal NSArray but when the nib is loaded it will be populated with your imageView's


Update

In the header file for you view controller which has the multiple imageView's on you need to add the property above - it may look something like this:

@interface MyViewController : UIViewController

@property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;
// other properties

@end

Now in the interface builder you connect all the imageView's to this one property.

Sample Image
Sample Image

Now I just work with the imageViews collection

for (UIImageView *imageView in self.imageViews) {
imageView.image = someImage;
}

Can multiple buttons have one outlet?

Sounds like you might be looking for IBOutletCollection (documentation linked for you).

I haven't actually made use of this yet, and it can only be used with an array of things. But I'm thinking these things (in the array) could be buttons.

How to change property for multiple IBOutlet

You can define a collection of IBOutlets in Swift, like this:

@IBOutlet var collectionOfTextFields: Array?

Use IB to add all the desired fields to collectionOfTextFields. Now you can use a simple loop to set attributes of all your text fields without writing a lot of code.

You should be able to go even further, and eliminate all your x1..x12 variables from the code by setting tag attribute of your UITextFields in IB, and them using these tags in your processing code to differentiate among the twelve fields in your interface.

How do I add UIImageViews to IBOutletCollection?

Try this:

NSMutableArray *tmpImages = [NSMutableArray arrayWithArray:self.imgs];
[tmpImages addObjectsFromArray:@[ smth1, smth2, smth3 ]];
self.imgs = tmpImages;

access to elements in IBOutletCollection

IBOutletCollection is an Array, hence to access its element we can use subscripts, like we do to access elements from normal array.

So to access its elements:

myCollection[0].image //or myCollection.first?.image 
myCollection[1].image
myCollection[2].image
....
myCollection.last?.image

How do use the properties of a UIImageView such us center.x and frame when it is an IBOutletCollection array?

The array is a collection of UIImageViews so you take an object off the array and query that.

Maybe...

for(UIImageView *xground in ground)
{

if (CGRectIntersectsRect(player.frame,xground.frame)) {
   //do stuff
break;

}

}

You ask each view in the array about its frame.

If you want to be really paranoid run a class check on the object to be sure it's an UIImageView coming off the array, but probably not necessary as your have already declared it as an array of UIImageView.

How to make IBOutlets out of an array of objects?

It is possible, it’s called outlet collection. This is the way to define an outlet collection:

@property(retain) IBOutletCollection(UIImageView) NSArray *images;

Now you can stick more than one object into the outlet in the Interface Builder, the array will be created for you when the interface is loaded.



Related Topics



Leave a reply



Submit