xcode 创建一个 for 循环以将 39 个按钮添加到数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5656432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a for loop to add 39 buttons to an array
提问by max_
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.
我的 .h 文件中有 39 个不同的 UIButton 变量,但我想将它们中的每一个都添加到一个数组中,而不必输入相同的内容 39 次。
Is there a way that I could do this in a for loop?
有没有办法在 for 循环中做到这一点?
The buttons are named accordingly: btn1,btn2,btn3 etc.
按钮相应地命名为:btn1、btn2、btn3 等。
回答by Moshe
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.
您可能希望放弃头文件中的 39 个按钮,而是使用单个数组。我怀疑您想使用手动引用,以便您可以利用 Interface Builder 来控制事件和布局。我建议做一些不同的事情。
Create a single property - an NSMutableArray
. Then, when the view loads, create the buttons on the fly.
创建单个属性 - 一个NSMutableArray
. 然后,当视图加载时,动态创建按钮。
To access a button, use something like [self.arrayOfButtons objectAtIndex:38];
. (In the case of 39 buttons, that would return the last button.);`
要访问按钮,请使用类似[self.arrayOfButtons objectAtIndex:38];
. (在 39 个按钮的情况下,将返回最后一个按钮。);`
To create a button, you use the following:
要创建按钮,请使用以下命令:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
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)
.
请注意,您传入了按钮init
方法的框架。您的按钮的框架将从其容器的左上角开始,您的按钮将是 100 像素的正方形。框架是 的一个实例CGRect
。(您CGRect
通过调用函数来创建一个CGRectMake(x,y,width,height)
。
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:
要制作 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];
}
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:
当然,您需要为、和每个按钮设置唯一值x
,否则它们都会重叠。创建按钮后,您可以对它们进行操作,例如设置标签或在屏幕上显示它们。要在屏幕上显示它们,您可以执行以下操作:y
width
height
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];
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.
第一部分,addTarget:self
,表示这个视图控制器处理您将要求它处理的事件。action:@selector(someMethod:)
告诉类在事件发生时执行什么方法。然后,forControlEvents:UIControlEventTouchDown
说当点击按钮时,所述类应该执行所述方法。
So, in your header:
所以,在你的标题中:
#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
And in your implementation, you can use this:
在你的实现中,你可以使用这个:
#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
Now, you can go to button paradise without taking Interface Builder on the plane. Go UIButton happy!
现在,不用带Interface Builder上飞机就可以去按钮天堂了。去 UIButton 快乐!
回答by Jacob Relkin
Like so:
像这样:
NSMutableArray *arr = [NSMutableArray array];
for(int i = 1; i <= 39; ++i) {
[arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
}
回答by Ben Scheirman
for(int i=1; i<totalButtonCount; i++) {
NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
[[NSClassFromString(buttonClassName) alloc] init];
//add to array
}
Edit: after re-reading your question, this might not be what you're asking. I thought you wanted to create a bunch of instances of similar classes.
编辑:重新阅读您的问题后,这可能不是您要问的。我以为你想创建一堆类似类的实例。
回答by Zaky German
You could create a getter method named after each button and than call [self valueForKey:]; for each but something here just screams "terrible design" :)
您可以创建一个以每个按钮命名的 getter 方法,然后调用 [self valueForKey:]; 对于每一个,但这里的东西只是尖叫“可怕的设计”:)
回答by Devraj
Key Value programmingis your friend
键值编程是你的朋友
Basically you can loop around and create your buttons, and as long as you have properties defined for your buttons you can.
基本上你可以循环创建你的按钮,只要你为你的按钮定义了属性,你就可以。
[self setObject:newButton forKey@"button1"];
where button1 is the same string as your variable name.
其中 button1 是与变量名称相同的字符串。