码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-08-27 22:28:18      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

  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

 

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

标签:

原文地址:http://www.cnblogs.com/wohaoxue/p/4764566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!