目标C:从数组创建uibutton时出错?

时间:2011-10-25 09:54:38

标签: iphone objective-c nsarray

我坚持以下代码:

btn1 = [UIButton alloc];
btn2 = [UIButton alloc];
btn3 = [UIButton alloc];
btn4 = [UIButton alloc];
btn5 = [UIButton alloc];
btn6 = [UIButton alloc];
NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil];

for (int i =0; i < [myarray count]; i++)
{
    NSLog(@"What: %@", [myarray objectAtIndex:i]); // Result UIButton
    [myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build
    UIButton *b = (UIButton *)[myarray objectAtIndex:i];
    b.frame = CGRectMake(i* 110+25, 60, 100, 100);
    [b setTitle:@"my button" forState:UIControlStateNormal];
    b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    b.titleLabel.textAlignment = UITextAlignmentCenter;
    b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0];
    b.tag = i+1;
    [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [self addSubview: b];
    [b release];
}

最初我使用switch case,如:

switch (i)
    case 0: {
        /*
        ..... 
        repeat this code
        .....
    }

因此,如果使用开关盒,代码很长。我循环它的原因是:

  • 职位
  • 尽量不要重复代码,例如设置标题,背景,添加目标等

或者您有更简单/更短的代码吗?

5 个答案:

答案 0 :(得分:3)

您已经分配了UIButtons但尚未初始化它们。

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];

您还需要在循环中缓存UIButton指针

UIButton *currentButton = [myarray objectAtIndex:i];

然后用'currentButton'替换'[myarray objectAtIndex:i]'的所有实例

答案 1 :(得分:0)

btn1 = [UIButton alloc];

你需要在alloc之后初始化,否则你刚刚分配了垃圾内存

btn1 = [[UIButton alloc] init];

另外

[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build

失败,因为它不知道对象是什么类,因此它是否可以调用setFrame:on,这就是你需要将它作为一个按钮(它是一个UIView子类)的原因

UIButton *b = (UIButton *)[myarray objectAtIndex:i];
b.frame = CGRectMake(i* 110+25, 60, 100, 100);

答案 2 :(得分:0)

你可以尝试一下吗?

for (int i =0; i < [myarray count]; i++)
{
    UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(i* 110+25, 60, 100, 100)];
    [b setTitle:@"my button" forState:UIControlStateNormal];
    b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    b.titleLabel.textAlignment = UITextAlignmentCenter;
    b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0];
    b.tag = i+1;
    [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [self addSubview: b];
    [b release];
}

我想你忘了初始化按钮了。有不同的方法来初始化它们。 init,initWithFrame和initWithCoder

答案 3 :(得分:0)

[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build 

由于您无法使用点语法来访问强制转换(或在本例中为id)对象,因此会出现错误。如果您删除此行并在设置b后放置以下内容:

b.frame = CGRectMake(i* 110+25, 60, 100, 100);

您将不再收到错误消息。

您还可以使用快速枚举来优化循环:

for (UIButton *b in myArray)

其他回答者指出您没有正确创建按钮是正确的 - 使用buttonWithTypeallocinitWithFrame

答案 4 :(得分:0)

UIButton *btn1 = [[UIButton alloc] init];
    UIButton *btn2 = [[UIButton alloc] init];
    UIButton *btn3 = [[UIButton alloc] init];
    UIButton *btn4 = [[UIButton alloc] init];
    UIButton *btn5 = [[UIButton alloc] init];
    UIButton *btn6 = [[UIButton alloc] init];
    NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil];

    for (int i =0; i < [myarray count]; i++)
    {
        NSLog(@"What: %@", [myarray objectAtIndex:i]);        
        UIButton *b = (UIButton *)[myarray objectAtIndex:i];
        b.frame = CGRectMake(i* 110+25, 60, 100, 100);
        [b setTitle:@"my button" forState:UIControlStateNormal];
        b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        b.titleLabel.textAlignment = UITextAlignmentCenter;
        float red = 0.33;
        float green = 0.82;
        float blue = 0.48;

        //Color values are float values between 0 and 1;

        b.titleLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
        b.tag = i+1;
        [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview: b];
        [b release];
    }