创建一个for循环,将39个按钮添加到一个数组中。

8

我在.h文件中有39个不同的UIButton变量,但我希望将它们全部添加到一个数组中,而不必重复输入39次相同的内容。

是否可以使用for循环来实现这一点?

这些按钮的名称分别为:btn1、btn2、btn3等。

5个回答

23

你可能想要放弃在头文件中使用39个按钮,并改为使用单个数组。我猜测你想要使用手动引用,以便利用Interface Builder来控制事件和布局。我建议做一些不同的事情。

创建一个单一的属性 - 一个NSMutableArray。然后,在视图加载时,动态创建按钮。

要访问一个按钮,请使用类似于[self.arrayOfButtons objectAtIndex:38];这样的方式。(在39个按钮的情况下,它将返回最后一个按钮。)

要创建一个按钮,使用以下代码:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

请注意,您需要在按钮的init方法中传入其框架。该按钮的框架将从其容器的左上角开始,且按钮将是100像素的正方形。框架是CGRect的实例。(您可以通过调用函数CGRectMake(x,y,width,height)来创建一个CGRect)。

要创建39个按钮,您可以使用以下循环,给定一个数组来保存按钮myButtons和预定义的numberOfButtons和维度变量:

for(int i=0;i<numberOfButtons;i++){
  //Create the button
  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];
  //Store the button in our array
  [self.myArray addObject:button];
  //Release the button (our array retains it for us)
  [button release];
}
当然,你需要为每个按钮设置唯一的 xywidthheight 值,否则它们都会重叠。创建完按钮后,你可以对它们进行各种操作,比如设置标签或在屏幕上显示。要在屏幕上显示按钮,你可以这样做:
for(UIButton *button in self.myButtons){
  //add the button to the view
  [self.view addSubview:button];
}

当然,仅仅在屏幕上添加按钮是没有意义的。您需要使它们具有可操作性。要为按钮添加操作,可以使用:

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
第一部分 addTarget:self 表示这个视图控制器将处理你要求它处理的事件。 action:@selector(someMethod:) 告诉类在事件发生时执行哪个方法。然后,forControlEvents:UIControlEventTouchDown表示当按钮被轻触时,该类应执行该方法。
因此,在您的头文件中:
#import <UIKit/UIKit.h>

@interface MyClass :UIViewController{

   NSMutableArray *myButtons; 
}

@property (nonatomic, retain) NSMutableArray *myButtons;

//Use UIButton as the sender type if you want 
- (void)someMethod:(id)sender;

// ... Other code can go here of course
@end

在您的实现中,您可以使用以下内容:

#import MyClass.h


@implementation MyClass

- (id)init{

  self = [super init];

  if(self){

     NSMutableArray *temp = [[NSMutableArray alloc] init];
     self.myButtons = temp;
     [temp release];

  }

  return self;

}


- (void)viewDidLoad{

  //
  // Create the buttons
  //

  for(int i=0;i<numberOfButtons;i++){
    //Create the button
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];

     //You may want to set button titles here
     //or perform other customizations

    //Store the button in our array
    [self.myArray addObject:button];
    //Release the button (our array retains it for us)
    [button release];
  }  

  //
  // Show the buttons onscreen
  //

  for(UIButton *button in self.myButtons){
    //add the button to the view
    [self.view addSubview:button];
    //add the action
    [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
  }
}

- (void)someMethod:(id)sender{
   //Do something when the button was pressed;
}

- (void)dealloc{
  [self.myButtons release];
  [super dealloc];
}

@end

现在,您可以在不需要将Interface Builder带上飞机的情况下前往按钮天堂。 开始享受UIButton的乐趣吧!


1
多么出色的方法啊!!我正在使用UIScrollView,所以设置框架最初会更加困难。非常感谢您花费时间在这上面,真的很感激! - max_
2
谢谢。实际上,在UIScrollView中设置框架并不难。UIScrollView有一个叫做contentSize的属性。这些框架是基于父视图的框架而定的。只需将它们添加到滚动视图中即可。 - Moshe
此外,在 Objective-C 中不存在 foreach 方法。 - max_
尝试发布一个新问题和示例代码,展示你尝试过什么以及哪些部分不起作用。 - Moshe

9

就像这样:

NSMutableArray *arr = [NSMutableArray array];
for(int i = 1; i <= 39; ++i) {
  [arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
}

4
for(int i=1; i<totalButtonCount; i++) {
   NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
   [[NSClassFromString(buttonClassName) alloc] init];
   //add to array
}

编辑:重新阅读您的问题后,我发现这可能不是您想要的。我认为您想创建一堆相似类的实例。

Scheriman,我在编辑之前就写了这个评论,这可能吗? :) - Zaky German
我立刻做到了。请原谅我之前的语气,昨天很...有压力。 ;) - Ben Scheirman
没关系。话说,我在你回答后很快就回复了,并且是用我的 iPhone 打字的,所以也许我甚至无法在打完字之前看到编辑;p 是啊,有些人会通过在 iPhone 上使用 SO 来获取声望:) - Zaky German

3
你可以为每个按钮创建一个getter方法,然后调用[self valueForKey:];但是这种设计非常糟糕 :)

完全同意。有些东西有点可疑。 - Ben Scheirman

2

键值编程是你的好朋友。

基本上,只要为按钮定义了属性,就可以循环创建按钮。

[self setObject:newButton forKey@"button1"];

其中,button1是与您的变量名称相同的字符串。


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