如果我在Interface Builder中设置了大量按钮,我如何通过代码将它们放入数组?
例如,如果我有三个按钮,并在界面文件中定义它们:
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;
如果我想将这些按钮重命名为“Hello”,我希望能够将它们组合成一个数组,然后使用如下函数:
for (int i = 0; i < someArray.count; i++)
{
someArray[i].text = @"Hello";
}
有人可以提供有关如何实现这一目标的信息(如果可能的话)吗?
由于
答案 0 :(得分:13)
另一种方法是将按钮连接到Interface Builder中的NSArray。
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
答案 1 :(得分:7)
以下代码将添加5个按钮,每个按钮都有一个新标签,并将其连接到一个事件。这些按钮将与5px填充并排放置:
如果您打算访问下面for{}
范围之外的按钮,则可以在标题中定义按钮,否则,您可以在范围内定义按钮。 UIButton * settButton;
CGFloat staticX = 5; // Static X for all buttons.
CGFloat staticWidth = 60; // Static Width for all Buttons.
CGFloat staticHeight = 56; // Static Height for all buttons.
CGFloat staticPadding = 5; // Padding to add between each button.
for (int i = 0; i < 5; i++)
{
settButton = [UIButton buttonWithType:UIButtonTypeCustom];
[settButton setTag:i];
[settButton setFrame:CGRectMake((staticX + (i * (staticHeight + staticPadding))),5,staticWidth,staticHeight)];
[settButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png",i]] forState:UIControlStateNormal];
[settButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchDown];
[settButtonView addSubview: settButton];
}
[self.view addSubview: settButtonView];
答案 2 :(得分:6)
假设IB中的按钮正确连接到IBOutlet属性,那么它就像以下一样简单:
NSArray *buttonArray = [[NSArray alloc] initWithObjects:button1,button2,button3,nil];
答案 3 :(得分:2)
由于您直接在Interface Builder中创建了按钮,您不能直接在代码中设置它们吗?
更通用的解决方案是这样的...如果我假设两件事:它们都是UIViewController视图的子视图,并且这些UIButton是该视图中的 only UIButtons,你可以做这样的事情:
for (UIView *aView in [self.view subviews]) {
if ([aView isKindOfClass:[UIButton class]) {
UIButton *aButton = (UIButton *) aButton;
[aButton setTitle:@"Hello" forState:UIControlStateNormal];
}
}
答案 4 :(得分:0)
我认为最简单的方法是创建一个您将初始化并添加到可变数组的按钮:
in * .m:
UIButton *buttonChar;
在您的函数中:
buttonArray = [[NSMutableArray alloc] init];
for (int i=0; i<lenghtWord; i++) {
buttonChar = [[UIButton alloc] initWithFrame:CGRectMake(.2*size.height+(float)i*charFontSize, .5*size.width, charFontSize, charFontSize)];
buttonChar.tag = i;
[buttonChar setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
[buttonChar setTitle:[[NSString alloc] formatWithString:@"Button %d",i] forState:UIControlStateNormal];
buttonChar.titleLabel.font = [UIFont boldSystemFontOfSize:15];
buttonChar.titleLabel.textAlignment = UITextAlignmentCenter;
buttonChar.layer.cornerRadius = 5;
buttonChar.layer.masksToBounds = YES;
buttonChar.layer.borderWidth = 1;
buttonChar.layer.borderColor = [[UIColor blackColor] CGColor];
[buttonChar setBackgroundColor:[UIColor whiteColor]];
[buttonChar addTarget:self action:@selector(buttonAction:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[buttonArray addObject:buttonChar];
}
按钮可通过以下方式获得:
[[buttonArray objectAtIndex:j] setTitle:@" " forState:UIControlStateNormal];
由于标签选项也被填充,我总是可以告诉如果显示了数组中的哪个按钮(buttonAction:withEvent :)
干杯