有许多按钮iOS的出口

Ann*_*nie 0 uibutton ios swift

我有一个计算器应用程序.如果用户按等于我想改变所有我的按钮(数字1至9)的背景颜色我应该具有用于​​每个按钮的出口(每个按钮表示一个数字)或有办法有一个出口,其覆盖所有按钮?

Dav*_*ood 7

你想要一个IBOutletCollection.

在Objective-C中,它看起来像这样:

@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray* buttons;
Run Code Online (Sandbox Code Playgroud)

在Swift中:

@IBOutlet var buttons: [UIButton] = []
Run Code Online (Sandbox Code Playgroud)

然后将每个按钮连接到Interface Builder中的按钮插座,您可以轻松地遍历每个按钮.