【发布时间】:2011-08-05 02:43:31
【问题描述】:
我的 .h 文件中有 39 个不同的 UIButton 变量,但我想将它们中的每一个都添加到一个数组中,而不必输入相同的内容 39 次。
有没有办法在 for 循环中做到这一点?
相应的按钮命名为:btn1、btn2、btn3 等。
【问题讨论】:
标签: iphone objective-c xcode loops uibutton
我的 .h 文件中有 39 个不同的 UIButton 变量,但我想将它们中的每一个都添加到一个数组中,而不必输入相同的内容 39 次。
有没有办法在 for 循环中做到这一点?
相应的按钮命名为:btn1、btn2、btn3 等。
【问题讨论】:
标签: iphone objective-c xcode loops uibutton
您可能希望放弃头文件中的 39 个按钮,而使用单个数组。 我怀疑您想使用手动引用,以便可以利用 Interface Builder 来控制事件和布局。我建议做一些不同的事情。
创建一个属性 - NSMutableArray。然后,当视图加载时,动态创建按钮。
要访问按钮,请使用 [self.arrayOfButtons objectAtIndex:38]; 之类的内容。 (如果有 39 个按钮,那将返回最后一个按钮。);`
要创建按钮,请使用以下内容:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 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,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、width 和 height 的唯一值,否则它们将全部重叠。创建按钮后,您可以对它们进行操作,例如设置标签或在屏幕上显示它们。要在屏幕上显示它们,您可以执行以下操作:
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,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 快乐!
【讨论】:
像这样:
NSMutableArray *arr = [NSMutableArray array];
for(int i = 1; i <= 39; ++i) {
[arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
}
【讨论】:
for(int i=1; i<totalButtonCount; i++) {
NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
[[NSClassFromString(buttonClassName) alloc] init];
//add to array
}
编辑:重新阅读您的问题后,这可能不是您要问的。我以为你想创建一堆类似类的实例。
【讨论】:
您可以创建一个以每个按钮命名的 getter 方法,然后调用 [self valueForKey:];对于每一个但这里的东西只是尖叫“糟糕的设计”:)
【讨论】:
基本上,您可以循环创建按钮,只要您为按钮定义了属性即可。
[self setObject:newButton forKey@"button1"];
其中 button1 是与变量名称相同的字符串。
【讨论】: