Objective C:从数组创建 uibuttons 时出错?

发布于 2024-12-11 22:28:19 字数 1318 浏览 0 评论 0原文

我被困在以下代码中:

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
        .....
    }

因此,如果使用 switch case,代码会很长。我循环它的原因是:

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

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

I am stuck at this following codes:

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];
}

Originally I use switch case, like:

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

Thus if use switch case, the codes are very long. The reason I loop it is:

  • The position
  • Try not to repeat the code, like set the title, background, add target etc

Or do you have a simpler/shorter code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黑色毁心梦 2024-12-18 22:28:19

您已分配 UIButton,但尚未初始化它们。

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

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

UIButton *currentButton = [myarray objectAtIndex:i];

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

You have alloced the UIButtons but have not initialized them.

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

You also need to cache the UIButton pointer in the loop

UIButton *currentButton = [myarray objectAtIndex:i];

then replace all instances of '[myarray objectAtIndex:i]' with 'currentButton'

北城挽邺 2024-12-18 22:28:19
btn1 = [UIButton alloc];

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

btn1 = [[UIButton alloc] init];

也会

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

失败,因为它不知道该对象是什么类,因此它是否可以在其上调用 setFrame: ,这就是为什么您需要将其转换为按钮(这是UIView 子类)

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

You need to init after alloc otherwise you have just allocated garbage memory

btn1 = [[UIButton alloc] init];

Also

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

fails because it does not know what class the object is and therefore if it can call setFrame: on it, which is why you need to cast it as a button (which is a UIView subclass)

UIButton *b = (UIButton *)[myarray objectAtIndex:i];
b.frame = CGRectMake(i* 110+25, 60, 100, 100);
起风了 2024-12-18 22:28:19

你可以试试这个吗?

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

You may try this?

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];
}

I think you forgot to initialize your buttons. There are different ways to initialize them. init, initWithFrame and initWithCoder

变身佩奇 2024-12-18 22:28:19
[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

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

Is giving you an error because you can't use dot syntax to access cast (or in this case, id) objects. If you remove this line and put the following after you have set b:

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

You will no longer get the error.

You could also optimise your loop using fast enumeration:

for (UIButton *b in myArray)

The other answerers are correct in pointing out that you aren't creating the buttons properly in the first place - use buttonWithType or alloc and initWithFrame.

寄意 2024-12-18 22:28:19
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];
    }
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];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文