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

| 我的.h文件中有39个不同的UIButton变量,但我想将每个变量添加到数组中,而不必键入相同的内容39次。 有没有办法可以在for循环中执行此操作? 这些按钮的名称分别为:btn1,btn2,btn3等。     
已邀请:
您可能希望放弃头文件中的39个按钮,而只使用一个数组。 我怀疑您想使用手动引用,以便可以利用Interface Builder来控制事件和布局。我建议做一些不同的事情。 创建一个属性-
NSMutableArray
。然后,在加载视图时,即时创建按钮。 要访问按钮,请使用ѭ1。 (对于39个按钮,将返回最后一个按钮。); 要创建按钮,请使用以下命令:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
请注意,您传入了按钮的
init
方法的框架。按钮的框架将从其容器的左上角开始,您的按钮将为100像素见方。框架是“ 4”的一个实例。 (您可以通过调用函数
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];
}
当然,您将需要为每个按钮设置unique10ѭ,
y
width
height
的唯一值,否则它们将重叠。创建按钮后,您可以对它们进行操作,例如设置标签或在屏幕上显示它们。要在屏幕上显示它们,您可以执行以下操作:
for(UIButton *button in self.myButtons){
  //add the button to the view
  [self.view addSubview:button];
}
当然,仅在屏幕上添加按钮是没有用的。您需要使他们能够做某事。要将操作添加到按钮,可以使用:
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
第一部分,ѭ16,表示此视图控制器处理您要要求其处理的事件。
action:@selector(someMethod:)
告诉类在事件发生时要执行的方法。然后,“ 18”表示当点击按钮时,所述类应执行所述方法。 因此,在标题中:
#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开心!     
像这样:
NSMutableArray *arr = [NSMutableArray array];
for(int i = 1; i <= 39; ++i) {
  [arr addObject:[self valueForKey:[NSString stringWithFormat:@\"btn%d\", i]]];
}
    
for(int i=1; i<totalButtonCount; i++) {
   NSString *buttonClassName = [NSString stringWithFormat:@\"btn%d\", i];
   [[NSClassFromString(buttonClassName) alloc] init];
   //add to array
}
编辑:重新阅读您的问题后,这可能不是您要问的。我以为您想创建一堆类似类的实例。     
您可以创建一个以每个按钮命名的getter方法,然后调用[self valueForKey:];对于每一个,但这里的东西只是尖叫“糟糕的设计” :)     
键值编程是您的朋友 基本上,您可以循环并创建按钮,并且只要您为按钮定义了属性即可。
[self setObject:newButton forKey@\"button1\"];
其中,button1与变量名相同。     

要回复问题请先登录注册