加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 创建一个for循环,为数组添加39个按钮

发布时间:2020-12-15 01:48:41 所属栏目:百科 来源:网络整理
导读:我的.h文件中有39个不同的UIButton变量,但我想将它们中的每一个添加到一个数组中,而不必输出相同的39次. 有没有办法可以在for循环中执行此操作? 按钮的名称相应:btn1,btn2,btn3等. 解决方法 您可能希望放弃头文件中的39个按钮,而是使用单个数组. 我怀疑您
我的.h文件中有39个不同的UIButton变量,但我想将它们中的每一个添加到一个数组中,而不必输出相同的39次.

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

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

解决方法

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

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

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

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

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(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,height)];
  //Store the button in our array
  [self.myArray addObject:button];
  //Release the button (our array retains it for us)
  [button release];
}

当然,您需要为每个按钮设置x,宽度和高度的唯一值,否则它们将全部重叠.创建按钮后,您可以使用它们进行操作,例如设置标签或在屏幕上显示它们.要在屏幕上显示它们,您可以执行以下操作:

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,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开心吧!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读