我在.h文件中有39个不同的UIButton变量,但我希望将它们全部添加到一个数组中,而不必重复输入39次相同的内容。
是否可以使用for循环来实现这一点?
这些按钮的名称分别为:btn1、btn2、btn3等。
我在.h文件中有39个不同的UIButton变量,但我希望将它们全部添加到一个数组中,而不必重复输入39次相同的内容。
是否可以使用for循环来实现这一点?
这些按钮的名称分别为:btn1、btn2、btn3等。
你可能想要放弃在头文件中使用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
}
键值编程是你的好朋友。
基本上,只要为按钮定义了属性,就可以循环创建按钮。
[self setObject:newButton forKey@"button1"];
其中,button1是与您的变量名称相同的字符串。