开发者

Is it possible to use something like an IBOutlet array?

开发者 https://www.devze.com 2022-12-14 10:33 出处:网络
I have a top list view in my current iPhone app, that\'s populated by code. I\'ve assembled the view using Interface Builder, so it contains lot of UILabels.

I have a top list view in my current iPhone app, that's populated by code. I've assembled the view using Interface Builder, so it contains lot of UILabels.

Obviously I wouldn't like to have name1, name2, etc. outlets in my class, but prefer a name[10]开发者_开发百科 array.

Is it possible to do so, and connect each item to the appropriate label (or any other interface builder like view)?


You can of course do this with interface builder, the keyword is IBOutletCollection. What it does is basically an NSArray out of multiple interface builder outlets.

IBOutletCollection(UILabel) NSArray *myLabels;

So the next thing would be connecting your labels in interface builder and then you can use the array to access all labels at runtime.


Follow these steps to create an array of outlets an connect it with IB Elements (Here is example of UIView, you can use UILabel also):

  • Create an array of IBOutlets
  • Add multiple UIElements (Views) in your Storyboard ViewController interface
  • Select ViewController (In storyboard) and open connection inspector
  • There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
  • Connect if with your interface elements

-

class ViewController2: UIViewController {


    @IBOutlet var collection:[UIView]!


    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

Is it possible to use something like an IBOutlet array?


you cannot do it in IB, but you can create an array in your init method and add all your labels to it.

BTW, you can set some tag to each label and define macro to access it. smth like

#define NAME[TAG] (UILabel*)[self.view viewWithTag:TAG]


This can be done using outlet collections, see this related question.

0

精彩评论

暂无评论...
验证码 换一张
取 消