我尝试从一组对象创建UIButtons。我使用
成功创建了这些float x=0,y=0;
for( NSMutableDictionary *dict in places)
{
NSLog(@"x: %f",x);
x=x+25;
y=y+25;// Vary these depending on where you want the buttons to be
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(x,y,25,25)] autorelease];
button.backgroundColor=[UIColor redColor];
[self addSubview:button];
}
但是我需要一些方法让所有这些按钮可以被我的类中的其他方法访问,特别是由CLLocation委托(或其他一些方式,甚至用户交互)触发的方法,并且可以为任何按钮设置动画。
希望有人能帮助我!
btw:对象“places”是一个MutableDictionaries数组。
谢谢!
答案 0 :(得分:0)
创建一个可变集合,用于保持对按钮的引用。如果需要以某种方式对它们进行排序,请创建NSMutableArray,否则创建NSMutableSet。在头文件中声明像 NSMutableSet buttonsSet 的ivar,然后在类实现中:
float x=0,y=0;
buttonsSet = [[NSMutableSet alloc] init];
for( NSMutableDictionary *dict in places) {
NSLog(@"x: %f",x);
x=x+25;
y=y+25;// Vary these depending on where you want the buttons to be
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(x,y,25,25)] autorelease];
button.backgroundColor=[UIColor redColor];
[buttonsSet addObject:button];
[self addSubview:button];
}
请注意,也可以通过 self.subviews 数组访问这些按钮。
答案 1 :(得分:0)
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(60, 327, 210, 32); // position in the parent view and set the size of the button
[myButton setTitle:@"Madhavan" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];