Objective-C:一个UIButton数组

7
如果我有大量通过 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";
}

请问有人能提供如何实现这个功能的信息吗(如果可能的话)?

谢谢


最简单的解决方案是在代码中创建按钮,而不是在界面构建器中创建。还有其他方法可以做到这一点,但没有一个特别好看。 - EricS
5个回答

14

另一种方法是在Interface Builder中将按钮连接到一个NSArray。

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

今天我学到了新的东西。我从来不知道这可以做到。太棒了!谢谢分享。 - Pavan
请注意,在故事板中,数组并不是有序的,但是在nib(xib)文件中它是有序的。 - vdd

8
以下代码将添加5个带有新标签的按钮,并将其连接到事件。这些按钮将并排放置,间距为5像素:
如果您计划在下面的for{}范围之外访问该按钮,则可以在头文件中定义您的按钮,否则可以在范围内定义它。UIButton * settButton; .h
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];

有用的帖子。我认为可能有一个打字错误。应该基于宽度设置框架x:[settButton setFrame:CGRectMake((staticX + (i * (staticWidth + staticPadding))),5,staticWidth,staticHeight)]; - DenVog
这真的取决于您对此的使用。它不是一个开箱即用的片段,您需要使其适合您的工作;更改框架以最好地适应您的情况需要完成。 - WrightsCS
1
@WrightsCS,我的需求不同,但是你的答案真的帮助我解决了问题。非常感谢! - Mihir Oza

6
假设 IB 中的按钮已正确地连接到 IBOutlet 属性,则操作非常简单:
NSArray *buttonArray = [[NSArray alloc] initWithObjects:button1,button2,button3,nil];

这是我希望做的事情。我现在无法测试它,但希望在回家测试之前将其写出来。如果它像这样简单,那就太棒了!谢谢。 - achiral
虽然这个应该可以正常工作 - 我认为 @Lachlan 的 IBOutletCollection 解决方案值得一看,可能更加适合。它允许您直接在界面生成器中进行布线。 - Paul.s
有一个问题,如果有人能够澄清一下。创建一个包含5个按钮的数组UIButton *buttons[5];,然后使用button[0] = ...是一个好的做法吗?在比较中也使用if (button[0] == currentButton)... - Petrescu Silviu

2

既然您是在Interface Builder中直接创建按钮,那么您不能直接在代码中设置它们吗?

更一般的解决方案可能是这样的...如果我假设两件事:它们都是UIViewController视图的子视图,并且这些UIButtons是该视图中唯一的UIButtons,您可以像这样做:

for (UIView *aView in [self.view subviews]) {
   if ([aView isKindOfClass:[UIButton class]) {
      UIButton *aButton = (UIButton *) aButton;
      [aButton setTitle:@"Hello" forState:UIControlStateNormal];
   }
}

0

我认为最简单的方法是创建一个单独的按钮,您将对其进行初始化并添加到可变数组中:

在*.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:),我总是可以知道哪个数组中的按钮被触摸了。

干杯


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接