如何重用属性

时间:2016-01-03 05:26:52

标签: ios objective-c class properties label

我的应用程序中有许多不同的按钮,但大多数按钮都分配了相同的属性:

login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[login setTitle:@"Login" forState:UIControlStateNormal];
[login.titleLabel setFont:[UIFont fontWithName:@"Avenir Next" size:18]];
[login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[login setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[login setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];

有没有办法创建一个已经分配了这些默认属性的类或按钮?所以我可以简单地说:

CustomButtom *btn = [CustomButton alloc]init];

然后btn会分配以上所有属性吗?

感谢。

4 个答案:

答案 0 :(得分:2)

另一种处理方法是,您可以创建一个私有方法,该方法将返回具有相同属性的UIButton。我认为创建一个UIButton的子类是有点不必要的。

答案 1 :(得分:0)

您可以通过创建 CustomButton

来完成此操作

Xcode - > 新文件 - > Cocoa Touch Class - > 下一步 - >命名你的按钮 - >选择 UIButton

子类

CustomButton.h文件

#import "CustomButton.h"

@implementation CustomButton


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[self setTitle:@"Login" forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:@"Avenir Next" size:18]];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];
}

@end 

CustomButton.m文件

CustomButton *customButton = [[CustomButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[customButton addTarget:self action:@selector(loginButtonPressed:) forControlEvents:UIControlEventTouchDown];
[YourView addSubview:customButton];

现在,给你打电话

tempInit = input()
temp = float(tempInit[:-1])
if tempInit[-1]== 'F':
    c=(temp-32)*5/9
    print(str(c)+'C')
if tempInit[-1]== 'C':
    f =(temp*9/5)+32
    print(str(f)+'F')

答案 2 :(得分:0)

您有两种选择:

答案 3 :(得分:-1)

是。您可以继承UIButton。当您覆盖init方法设置属性时,您可以获得具有相同属性的按钮。