在Objective-C中以编程方式创建UI元素 [英] Creating UI elements programmatically in Objective-C

查看:49
本文介绍了在Objective-C中以编程方式创建UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在致力于自动化创建UI元素的某些方面,并创建了这种似乎可行的方法:

I have been working on automating some aspects of creating UI elements and have created this method that seems to work:

    - (void) viewDidLoad
        {
            [super viewDidLoad];
            button = [self createButton:button xPos:30 yPos:100 width:100 height:30  caption:@"autoButton" textPos:NSTextAlignmentCenter textClr:[UIColor blackColor] backClr:[UIColor yellowColor]];
            [self.view addSubview: button];
        }

    - (UIButton *) createButton:(UIButton *)control xPos:(CGFloat)x yPos:(CGFloat)y width:(CGFloat)width height:(CGFloat)height caption:(NSString *)caption textPos:(NSTextAlignmentCenter)textPosition textClr:(UIColor *)textColor backClr:(UIColor *)backColor
        {
            control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
            [control setTitle:caption forState:UIControlStateNormal];
            control.titleLabel.textAlignment = textPosition;
            control.backgroundColor = backColor;
            [control setTitleColor: textColor forState:UIControlStateNormal];
            return control;
        }

  1. 此实现有什么错误/不好吗?

  1. Is there anything wrong/bad with this implementation?

不是使用方法,仅使用简单的旧函数就可以实现相同的东西吗?必须指出参数名称有些麻烦, xPos:30 yPos:100 width:100 height:100,等等.

Instead of using methods, would it be possible to implement the same thing with just plain old functions? It's a little long-winded to have to indicate the parameter names, xPos:30 yPos:100 width:100 height:100, etc.

如果能够执行以下操作就好了:

Would be nice to be able to just do something like:

button = createButton(30, 100, 100, 30, @"autoButton", NSTextAlignmentCenter, [UIColor blackColor], [UIColor yellowColor]);

这可行吗?

推荐答案

  1. 精细,但没有必要传递 button 参数.只需返回 UIButton 对象.

- (void) viewDidLoad
    {
        [super viewDidLoad];
        button = [self createButtonWithxPos:30 yPos:100 width:100 height:30  caption:@"autoButton" textPos:NSTextAlignmentCenter textClr:[UIColor blackColor] backClr:[UIColor yellowColor]];
        [self.view addSubview: button];
    }

- (UIButton *) createButtonWithxPos:(CGFloat)x yPos:(CGFloat)y width:(CGFloat)width height:(CGFloat)height caption:(NSString *)caption textPos:(NSTextAlignmentCenter)textPosition textClr:(UIColor *)textColor backClr:(UIColor *)backColor
    {
        UIButton *control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
        [control setTitle:caption forState:UIControlStateNormal];
        control.titleLabel.textAlignment = textPosition;
        control.backgroundColor = backColor;
        [control setTitleColor: textColor forState:UIControlStateNormal];
        return control;
    }

  • 好,使用函数或方法.由你决定.只要不需要 self ,函数就可以工作.

    UIButton *createButton(CGFloat x, CGFloat y, CGFloat width, CGFloat height, NSString *caption, NSTextAlignmentCenter textPosition, UIColor *textColor, UIColor *backColor) {
        UIButton *control = [[UIButton alloc] initWithFrame: CGRectMake(x, y, width, height)];
        [control setTitle:caption forState:UIControlStateNormal];
        control.titleLabel.textAlignment = textPosition;
        control.backgroundColor = backColor;
        [control setTitleColor: textColor forState:UIControlStateNormal];
        return control;
    }
    
    - (void) viewDidLoad
        {
            [super viewDidLoad];
            button = createButton(30, 100, 100, 30, @"autoButton", NSTextAlignmentCenter, [UIColor blackColor], [UIColor yellowColor]);
            [self.view addSubview: button];
        }
    

  • 这篇关于在Objective-C中以编程方式创建UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆