您的位置:首页 > 产品设计 > UI/UE

iOS中自定义UIView(用接口获取Lable和TextFile中的值)

2015-08-27 20:45 661 查看
NSArray *arrayText = @[@"用户名",@"密码",@"确认密码",@"手机号",@"邮箱"];

NSArray *placeholders = @[@"请输入用户名",@"请输入密码",@"请确认密码",@"请输入手机号",@"请输入邮箱"];
NSInteger y = 30;
for (int i = 0; i < 5; i++) {

LTView *aView = [[LTView alloc] initWithFrame:CGRectMake(30, y, 280, 35)];

//aView.backgroundColor = [UIColor redColor];

[aView setDesLabelText:arrayText[i]];
[aView setPlacehloder:placeholders[i]];
[self.window addSubview:aView];
[aView setDelegate:self];

[aView release];

if (i == 1 || i ==2) {

[aView setSecureTextType:YES];
}

if (i == 3) {
[aView setKeyboardType:UIKeyboardTypeNumberPad];
}
y += 45;

}

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

[btn setTitle:@"注册" forState:UIControlStateNormal];

btn.frame = CGRectMake(50, 270, 100, 40);

//btn.backgroundColor = [UIColor redColor];

[view addSubview:btn];

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];

[btn1 setTitle:@"取消" forState:UIControlStateNormal];

btn1.frame = CGRectMake(170, 270, 100, 40);

//btn1.backgroundColor = [UIColor redColor];
[view addSubview:btn1];

[btn addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];

[btn1 addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

- (void)quit
{
//NSLog(@"-----");
//UIView *view = [self.window viewWithTag:50];
//    for (int i = 0; i < 5; i++) {
//        UITextField *fil = (UITextField *)[view viewWithTag:(100 * i) + 100];
//        fil.text = @"";
//    }
[self.window endEditing:YES];

}

- (void)zhuce
{
//     UIView *view = [self.window viewWithTag:50];
//    for (int i = 0; i < 5; i++) {
//        UITextField *fil = (UITextField *)[view viewWithTag:(100 * i) + 100];
//        fil.text = @"=-====顶你肺==";
//    }

}


LTView.h(自定义的UIView,定义接口)

@interface LTView : UIView

//LTView 属于自定义的视图(组合视图),携带有坐label,又textField,两个控件只有LTView能狗识别,因此需要LTView给外界提供操作label和textFiedld访问接口
//给label设置文字的接口

- (void)setDesLabelText:(NSString *)text;

//给textField设置提示文字
- (void)setPlacehloder:(NSString *)text;

//给txtField 设置键盘接口
- (void)setKeyboardType:(UIKeyboardType)keyboardType;

//给textField设置密码格式
- (void)setSecureTextType:(BOOL)secureTextType;

- (NSString *)text;

//给textField设置代理的接口
- (void)setDelegate:(id<UITextFieldDelegate>)delegate;

@end


LTView.m(关键在重写initWithFrame方法)

#import "LTView.h"
@interface LTView ()
@property (nonatomic,retain) UILabel *desLabel;
@property (nonatomic,retain) UITextField *textField;
@end

@implementation LTView

/*

重写从父类继承来的initWithFrame:方法,在自定义视图初始化的时候完成自身控件的创建

*/
- (instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
//完成自身控件的创建

//label
[self setDescLabelWithWidth:frame.size.width / 3 height:frame.size.height];
//textField
[self setPlacehodlTextFieldWithWidth:frame.size.width / 3 * 2 height:frame.size.height];
}
return self;
}

//创建label
- (void)setDescLabelWithWidth:(CGFloat)width height:(CGFloat)height
{
self.desLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
//self.desLabel.backgroundColor = [UIColor redColor];
[self addSubview:self.desLabel];

[self.desLabel release];

}

//创建textField
- (void)setPlacehodlTextFieldWithWidth:(CGFloat)width height:(CGFloat)height
{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(width / 2  , 0, width, height)];
self.textField.borderStyle =UITextBorderStyleRoundedRect;
// textfield.backgroundColor = [UIColor yellowColor];
[self addSubview:self.textField];
[self.textField release];
}

//给label设置文字的接口

- (void)setDesLabelText:(NSString *)text
{
self.desLabel.text = text;
}

//给textField设置提示文字
- (void)setPlacehloder:(NSString *)text
{
self.textField.placeholder = text;
}

//给txtField 设置键盘接口
- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
self.textField.keyboardType = keyboardType;
}

//给textField设置密码格式
- (void)setSecureTextType:(BOOL)secureTextType
{
self.textField.secureTextEntry = secureTextType;
}
//获取textField中的文字
- (NSString *)text{
return self.textField.text;
}
//给textField设置代理的接口
- (void)setDelegate:(id<UITextFieldDelegate>)delegate
{
self.textField.delegate = delegate;
//外界传入的参数是谁,谁就是这个textField的代理
}

- (void)dealloc
{
[_desLabel release];
[_textField release];
[super dealloc];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: