IBOutletCollection When connecting IB with related files, we often use two keywords: IBOutlet and IBAction. Those who often use xib or storyboard should be very familiar with these two keywords. However, UIKit also provides another pseudo keyword IBOutletCollection. With this keyword, we can connect a group of identical controls on the interface to the same array . Let's first look at the definition of this pseudo keyword. You can find the following definition from the header file UINibDeclarations.h of UIKit.framework:
In addition, in the Clang source code, there is a safer definition method, as shown below:
From the above definition, you can see that, unlike IBOutlet, IBOutletCollection takes a parameter, which is a class name. Normally, when we use an IBOutletCollection property, the property must be strong and of type NSArray, as shown below:
Assuming that there are three horizontal scrollViews in our xib file, we can connect all three scrollViews to the scrollViews property, and then we can do some unified processing in our code, as shown below:
This code will affect three scrollViews. The advantage of doing this is that we don't need to manually add the scrollView to the scrollViews through the addObject: method. However, when using IBOutletCollection, you need to pay attention to two points: The order of objects in an IBOutletCollection is undefined. We can see from the debugger that the order of objects in the collection is the same as the order in which we connected them. But this order may vary between versions of Xcode. So we should not try to assume this order in our code. No matter what the control in IBOutletCollection(ClassName) is, the type of the property is always NSArray. In fact, we can declare any type, such as NSSet, NSMutableArray, or even UIColor, but no matter what class we set here, the IBOutletCollection property always points to an NSArray array. Regarding the second point, let's take the scrollViews above as an example and make the following changes:
In fact, when we print this scrollViews in the console, the result is as follows:
As you can see, it points to an NSArray array. In addition, IBOutletCollection actually existed in iOS 4. However, Objective-C now supports object literals, so you can define arrays directly with @[], which is much more convenient. And object literals can add views defined in code that are not in the xib, so it is more flexible. Of course, which of the two methods to choose depends on our actual needs and preferences. refer to IBAction / IBOutlet / IBOutletCollection IBOutletCollection.m |
<<: When should I use copy and when should I use strong for NSString attributes?
>>: Different ways to implement locks in Objective-C (Part 2)
If you want to continue to get the old results, c...
Link cheating is a despicable behavior in SEO. Li...
At present, my country's economy is at a crit...
This article will not explain how to use the Fren...
Recently, a video of an unlucky young man being b...
The competition in the PC-based "show" ...
The end of the year is approaching and various ac...
In order to write this article well, I have been ...
In our daily life, we have all received 400 calls...
In 2019, affected by the sluggish macroeconomic s...
Produced by: Science Popularization China Produce...
In 1865, on the flat land outside Yongdingmen in ...
Mini programs are all developed by companies with...
After we schedule a short video on Douyin, we nee...
Previously, some parents gave their children sex ...