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

查看:126
本文介绍了创建一个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];
}

当然,您需要为 x , y 宽度高度或它们都会重叠。一旦你创建了按钮,就可以用它们做一些事情,比如设置标签,或者在屏幕上显示它们。要在屏幕上显示它们,你可以做类似这样的事情:

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天全站免登陆