创建一个 for 循环以将 39 个按钮添加到数组中 [英] Create a for loop to add 39 buttons to an array

查看:31
本文介绍了创建一个 for 循环以将 39 个按钮添加到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 39 different UIButton variables in my .h file, but I would like to add each of them to an array without having to type out the same thing 39 times.

有没有办法可以在 for 循环中做到这一点?

Is there a way that I could do this in a for loop?

相应的按钮命名为:btn1、btn2、btn3 等.

The buttons are named accordingly: btn1,btn2,btn3 etc.

推荐答案

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

You might want to forego the 39 buttons in your header file and instead have a single array. I suspect that you want to use manual references so you can take advantage of Interface Builder, to control events and layout. I suggest doing something a little different.

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

Create a single property - an NSMutableArray. Then, when the view loads, create the buttons on the fly.

要访问按钮,请使用 [self.arrayOfButtons objectAtIndex:38]; 之类的内容.(如果有 39 个按钮,那将返回最后一个按钮.);`

To access a button, use something like [self.arrayOfButtons objectAtIndex:38];. (In the case of 39 buttons, that would return the last button.);`

要创建按钮,请使用以下内容:

To create a button, you use the following:

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

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

Note that you pass in the frame of your button's init method. The frame of your button is going to start in the top left corner of its container and your button will be 100 pixels square. The frame is an instance of CGRect. (You create a CGRect by calling the function CGRectMake(x,y,width,height).

要制作 39 个按钮,您可能需要如下循环,给定一个数组来保存按钮、myButtons 和预定义的 numberOfButtons 和维度变量:

To make 39 buttons, you might want to loop as follows, given an array to hold the buttons, myButtons and predefined numberOfButtons and dimension variables:

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 设置唯一值对于每个按钮,否则它们都会重叠.创建按钮后,您可以对它们进行操作,例如设置标签或在屏幕上显示它们.要在屏幕上显示它们,您可以执行以下操作:

Of course, you are going need to set unique values for x,y,width and height for each button or they will all overlap. Once you've created your buttons, you can do things with them, like set the label, or show them onscreen. To show them onscreen, you can do something like this:

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

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

Of course, just adding buttons to the screen is useless. You need to be able to make them do something. To add an action to a button, you can use:

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];

第一部分,addTarget:self,表示此视图控制器处理您将要求它处理的事件.action:@selector(someMethod:) 告诉类在事件发生时执行什么方法.然后,forControlEvents:UIControlEventTouchDown 表示当点击按钮时,该类应该执行该方法.

The first part, addTarget:self, says that this view controller handles the event that you're going to ask it to handle. action:@selector(someMethod:) tells the class what method to perform when the event occurs. Then, forControlEvents:UIControlEventTouchDown says that the said class should perform the said method when the button is tapped.

所以,在你的标题中:

#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 快乐!

Now, you can go to button paradise without taking Interface Builder on the plane. Go UIButton happy!

这篇关于创建一个 for 循环以将 39 个按钮添加到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆