我的.h文件中有39个不同的UIButton变量,但是我想将它们中的每一个都添加到数组中,而不必输入相同的东西39次。创建一个for循环,将39个按钮添加到数组中
有没有一种方法可以在for循环中做到这一点?
的按钮会被相应命名为:BTN1,BTN2,btn3等
我的.h文件中有39个不同的UIButton变量,但是我想将它们中的每一个都添加到数组中,而不必输入相同的东西39次。创建一个for循环,将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
现在,你可以去按钮天堂没有考虑界面生成器在飞机上。去UIButton快乐!
基本上就可以循环并创建你的按钮,只要你有你的按钮定义的属性您可以。
[self setObject:newButton [email protected]"button1"];
其中button1是与变量名称相同的字符串。
像这样:
NSMutableArray *arr = [NSMutableArray array];
for(int i = 1; i <= 39; ++i) {
[arr addObject:[self valueForKey:[NSString stringWithFormat:@"btn%d", i]]];
}
嘿雅各,欢迎回来! – Moshe 2011-04-13 22:34:24
@Moshe谢谢! :) – 2011-04-13 22:37:13
您可以创建每个按钮后不是打电话叫一个getter方法[自valueForKey:]。每个但这里的东西只是尖叫“可怕的设计” :)
完全同意。有些东西是臭的。 – 2011-04-13 22:34:38
for(int i=1; i<totalButtonCount; i++) {
NSString *buttonClassName = [NSString stringWithFormat:@"btn%d", i];
[[NSClassFromString(buttonClassName) alloc] init];
//add to array
}
编辑:后重读你的问题,这可能不是你在问什么。我以为你想创建一堆类似类的实例。
是的,这很明显。你读过我的回答了吗? – 2011-04-14 04:25:24
Scheriman是否有可能在编辑之前编写了评论? :) – 2011-04-14 06:17:34
我立即做到了。请原谅我以前的口气,昨天......压力很大。 ;) – 2011-04-14 18:59:17
多么辉煌的做法!我使用UIScrollView,因此设置框架最初会更困难。非常感谢您花时间在此,非常感谢! – 2011-04-13 23:24:46
谢谢。实际上,在UIScrollView中设置帧并不困难。 UIScrollView有一个名为contentSize的属性。这些框架基于父视图的框架。只需将它们添加到滚动视图。 – Moshe 2011-04-13 23:36:07
另外,foreach方法在objective-c中不存在。 – 2011-08-16 10:47:38