Iphone 我可以将按钮的对象添加到NSMutableArray吗?
我试图以编程方式在视图上创建多个按钮,为此我需要数组,因此我可以在Iphone 我可以将按钮的对象添加到NSMutableArray吗?,iphone,Iphone,我试图以编程方式在视图上创建多个按钮,为此我需要数组,因此我可以在NSMutableArray?中添加按钮对象吗?当然可以,请记住,当您这样做时,每个按钮或其他对象都将保留在数组中,并在您将其从数组中移除时释放…是的,可以容纳任何对象是的,您可以这样做,请参见以下代码 // Create buttons for the sliding menu. For simplicity I create 5 standard buttons. NSMutableArray *buttonArray =
NSMutableArray
?中添加按钮对象吗?当然可以,请记住,当您这样做时,每个按钮或其他对象都将保留在数组中,并在您将其从数组中移除时释放…是的,可以容纳任何对象是的,您可以这样做,请参见以下代码
// Create buttons for the sliding menu. For simplicity I create 5 standard buttons.
NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
for(NSInteger i = 0; i < [self.slideMenuArray count]; i++)
{
// Rounded rect is nice
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *title=[slideMenuArray objectAtIndex:i];
[btn setFrame:CGRectMake(0.0f, 4.0f, 90.0f, 20.0f)];
[btn setTitle:[NSString stringWithString:title] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[[btn titleLabel] setFont:[UIFont systemFontOfSize:12]];
[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *backgroundView;
if(i==0)
backgroundView= [UIImage imageNamed:@"btnclk.png"];
else
backgroundView= [UIImage imageNamed:@"btn.png"];
[btn setBackgroundImage:backgroundView forState:UIControlStateNormal];
[buttonArray addObject:btn];
}
//为滑动菜单创建按钮。为了简单起见,我创建了5个标准按钮。
NSMutableArray*buttonArray=[[NSMutableArray alloc]init];
对于(NSInteger i=0;i<[self.slideMenuArray count];i++)
{
//圆形矩形很好
UIButton*btn=[UIButton按钮类型:UIButtonTypeCustom];
NSString*title=[slideMenuArray objectAtIndex:i];
[btn设置帧:CGRectMake(0.0f、4.0f、90.0f、20.0f)];
[btn setTitle:[NSString stringWithString:title]用于状态:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setTitleColor:[UIColor whiteColor]用于状态:UIControlStateNormal];
[[btn titleLabel]setFont:[UIFont systemFontOfSize:12]];
[btn addTarget:self action:@selector(按钮按下:)for ControlEvents:UIControlEventTouchUpInside];
UIImage*背景视图;
如果(i==0)
backgroundView=[UIImage ImageName:@“btnclk.png”];
其他的
backgroundView=[UIImage ImageName:@“btn.png”];
[btn setBackgroundImage:状态的背景视图:UIControlStateNormal];
[buttonArray添加对象:btn];
}
您可以像这样检索添加的按钮对象-
for(int i = 0; i < [buttonArray count]; i++)
{
UIButton *btn = [buttonArray objectAtIndex:i];
// Move the buttons position in the x-demension (horizontal).
CGRect btnRect = btn.frame;
btnRect.origin.x = totalButtonWidth;
[btn setFrame:btnRect];
}
for(int i=0;i<[buttonArray count];i++)
{
UIButton*btn=[buttonArray对象索引:i];
//在x尺寸(水平)中移动按钮位置。
CGRect btnRect=btn.frame;
btnRect.origin.x=总按钮宽度;
[btn设置帧:btnRect];
}