How to make IBOutlets out of an array of objects?

后端 未结 5 1378
南旧
南旧 2020-12-30 20:13

I want to make an array with a bunch of UIImageViews I have in Interface Builder. Instead of having 20 or 30

IBOutlet UIImageView *img1;

a

5条回答
  •  温柔的废话
    2020-12-30 21:05

    There's not, unfortunately, but you can keep all of the declarations on a single line:

    IBOutlet UIImageView *img1, *img2, *img3, *img4;
    

    The other option (probably best, since you have so many of these) would be to create them programatically and store them in an array, then add them to the view from your view controller class, using, for each,

    [self.view addSubview:img];
    

    Also, keep in mind that if the elements are static (like background elements), and you don't actually need to access them, you don't need to declare outlets for each; you can just add them to the nib file and forget about them.

    Same goes for UIButton instances. If you don't need to change anything about the button, you can access it from the method that it calls, like so:

    -(IBAction) buttonPressed:(id)sender {
        UIButton *button = (UIButton *)sender;
        // method guts
        // stuff with button -- access tag, disable, etc
    }
    

提交回复
热议问题