创建一个for循环以向数组添加39个按钮

max*_*ax_ 8 iphone xcode loops objective-c uibutton

我的.h文件中有39个不同的UIButton变量,但我想将它们中的每一个添加到一个数组中,而不必输入相同的39次.

有没有办法可以在for循环中执行此操作?

按钮的名称相应:btn1,btn2,btn3等.

Mos*_*she 23

您可能希望放弃头文件中的39个按钮,而是使用单个数组.我怀疑您希望使用手动引用,以便您可以利用Interface Builder来控制事件和布局.我建议做一些不同的事情.

创建一个属性 - 一个NSMutableArray.然后,当视图加载时,动态创建按钮.

要访问按钮,请使用类似的功能[self.arrayOfButtons objectAtIndex:38];.(在39个按钮的情况下,将返回最后一个按钮.);`

要创建按钮,请使用以下命令:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
Run Code Online (Sandbox Code Playgroud)

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

要创建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];
}
Run Code Online (Sandbox Code Playgroud)

当然,你将需要为组唯一值x,y,widthheight为每个按钮或就会全部重叠.创建按钮后,您可以使用它们进行操作,例如设置标签或在屏幕上显示它们.要在屏幕上显示它们,您可以执行以下操作:

for(UIButton *button in self.myButtons){
  //add the button to the view
  [self.view addSubview:button];
}
Run Code Online (Sandbox Code Playgroud)

当然,只是在屏幕上添加按钮是没用的.你需要能够让他们做点什么.要向按钮添加操作,您可以使用:

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
Run Code Online (Sandbox Code Playgroud)

第一部分,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
Run Code Online (Sandbox Code Playgroud)

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

#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
Run Code Online (Sandbox Code Playgroud)

现在,您无需在飞机上使用Interface Builder即可进入按钮天堂.去UIButton开心吧!

  • 谢谢.实际上,在UIScrollView中设置帧并不是那么难.UIScrollView有一个名为contentSize的属性.帧基于父视图的帧.只需将它们添加到滚动视图即可. (2认同)

Jac*_*kin 9

像这样:

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