Connect items to a (specific index in) an array from Interface Builder

不羁岁月 提交于 2020-01-06 05:48:07

问题


This one ought to be a softball for the Objective-C pros out there:

Is there a way to connect an interface builder object to an element of an NSArray in Objective-C? The connecting would normally be done with IBOutlet, as in:

@interface ViewController : UIViewController {
    IBOutlet UILabel *label1;
    IBOutlet UILabel *label2;
    IBOutlet UILabel *label3;
    //...
}

Can I put the labels in an NSArray and still attach them to objects in interface builder?


回答1:


You should be able to use IBOutletCollection, which is essentially an array of IBOutlets. Use this property declaration:

@property (nonatomic, retain) IBOutletCollection(/*whatever object you want an array of e.g. UILabel*/) NSArray *seats;

You will be able to connect multiple objects of one type to this in Interface builder.



来源:https://stackoverflow.com/questions/4402801/connect-items-to-a-specific-index-in-an-array-from-interface-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!