xcode 如何将对象添加到 IBOutletCollection?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8326249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add objects to IBOutletCollection?
提问by Andrey Chernukha
I declared NSArray in my class
我在班级中声明了 NSArray
NSArray *labelsArray;
I made it a property
我把它变成了财产
@property (nonatomic,retain) IBOutletCollection(UILabel) NSArray *labelsArray;
I connected it to four UILabels in IB. I allocated the array. When i do this
我将它连接到 IB 中的四个 UILabels。我分配了数组。当我这样做时
NSLog(@"labelsArray.count %i",[labelsArray count]);
It tells me that labelsArray's count is 0. What should i do to actually add those labels to the array?
它告诉我labelsArray 的计数为0。我应该怎么做才能将这些标签实际添加到数组中?
采纳答案by jbat100
I allocated the array.
我分配了数组。
It could be that the array is automatically instantiated for you when the NIB file is loaded and that reallocating it creates a new (empty) version of the array. Try not allocating it. Also make sure you NSLog the array in viewDidLoad
, when the IB elements are loaded.
可能是在加载 NIB 文件时为您自动实例化该数组,并且重新分配它会创建一个新的(空)版本的数组。尽量不要分配它。还要确保在viewDidLoad
加载 IB 元素时NSLog 中的数组。
回答by Rayfleck
Where are you calling the NSLog statement? The array will not be instantiated until viewDidLoad is called.
你在哪里调用 NSLog 语句?在调用 viewDidLoad 之前,不会实例化该数组。