【问题标题】:Creating UIButton programmatically with custom class使用自定义类以编程方式创建 UIButton
【发布时间】:2013-11-22 08:58:49
【问题描述】:

我想以编程方式创建 3-4 个按钮,但要使用自定义类和特定的值/键对,但我很难准确地做到这一点。 我要创建的按钮必须有一个名为“AnswerButton”的自定义类。添加一个 UIButton.tag 应该不是问题,所以我可以准确地知道点击了哪个按钮,对吧?

这是我用来创建按钮的代码:

NSMutableArray *catNames;
catNames = [NSMutableArray arrayWithCapacity:5];
[catNames addObject:@"Boote"];
[catNames addObject:@"Gewässer"];
[catNames addObject:@"Technik"];
[catNames addObject:@"Krims Krams"];

[self dynamiclyCreateButtons:4 :catNames];

- (void)dynamiclyCreateButtons:(int)howMany :(NSMutableArray*)catNames {
    float standard_btnHeight = 30.0;
    float standard_btnWidth = 200.0;
    CGFloat p = 120;

    for(int i = 0; i != howMany; i++){
        UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [catBtn setFrame:CGRectMake(0.0f, 0.0f, standard_btnWidth, standard_btnHeight)];
        [catBtn setCenter:CGPointMake(100.0f, p)];
        [catBtn setTitle:[catNames objectAtIndex:i] forState:UIControlStateNormal];
        [catBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:catBtn];
        p=p+40;
        catBtn = nil;
    }
}

//编辑 我所说的自定义类是什么意思: https://i.stack.imgur.com/8r2oz.png

//编辑2(正确答案,因为我无法自己发布答案以更好地指出)

只是在这里指出并更容易找到答案:Greg 的回答是正确的。 您不应该使用默认类创建按钮,而是使用您的自定义类:

替换

UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

AnswerButton *catBtn = [AnswerButton buttonWithType:UIButtonTypeRoundedRect];

别忘了导入类!

【问题讨论】:

  • 自定义类是什么意思?你想用“AnswerButton”类创建所有按钮,还是想用不同的类创建每个按钮?
  • 您是否正在寻找从 UIButton 派生的名为 AnswerButton 的类?
  • @Greg 我希望所有创建的 UIButtons 都具有自定义类“AnswerButton”。请参阅第一篇文章中的屏幕截图。
  • @HRM 不,AnswerButton 是我在项目中拥有的自己的课程。我希望按钮有这个自定义类,所以我可以使用“用户定义的运行时属性”
  • 替换你的 UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; with AnswerButton *catBtn = [AnswerButton buttonWithType:UIButtonTypeRoundedRect];

标签: ios objective-c uibutton


【解决方案1】:

试试这个...

-(void)dynamiclyCreateButtons:(int)howMany :(NSMutableArray*)catNames {
    float standard_btnHeight = 30.0;
    float standard_btnWidth = 200.0;
    CGFloat p = 120;


    for(int i = 0; i != howMany; i++){
        UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [catBtn setFrame:CGRectMake(0.0f, 0.0f, standard_btnWidth, standard_btnHeight)];
        [catBtn setCenter:CGPointMake(100.0f, p)];
        [catBtn setTag:i];
        [catBtn setTitle:[catNames objectAtIndex:i] forState:UIControlStateNormal];
        [catBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:catBtn];
        p=p+40;
        catBtn = nil;
    }
}
- (IBAction)buttonClicked:(id)sender
{
    NSLog(@"Button.Tag = %d",[sender tag]);
}

编辑:如果你想使用自定义类,你必须创建一个继承自UIButton 的类。然后在你的视图控制器中导入这个类。

之后将UIButton 替换为YourCustomButton like..

YourCustomButton *catBtn = [YourCustomButton buttonWithType:UIButtonTypeRoundedRect];

【讨论】:

  • 谢谢,TAG 的东西现在可以工作了。关于如何将类添加到该按钮的任何想法?
  • 欢迎您。我听不懂你想说“添加课程”。
  • 格雷格为我指出了正确的方向。请参阅我在主帖中的第二次编辑。抱歉,如果一开始就不够清楚。
  • 查看我的编辑。它可能会帮助你。在你的第一篇文章中并不太清楚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多